Customer Order Dashboard - Backend API
A professional Node.js/Express backend API for the Customer Order Dashboard application, following industry-standard architecture and best practices.
List All Customers - Paginated list with search and filtering
Customer Details - Complete profile with order statistics
Advanced Search - Complex filtering with multiple criteria
Customer Analytics - Demographics and behavior insights
π Advanced Filtering & Search
Text Search : Name, email search
Location Filters : State, city, country
Demographics : Age range, gender
Behavior : Traffic source, registration date
Sorting : Multiple fields with asc/desc order
π Analytics & Statistics
Customer demographics breakdown
Registration trends by period
Geographic distribution
Traffic source analysis
Customer lifetime value metrics
π Backend Architecture
βββ π― Controllers β Request handling & response formatting
βββ π Models β Database schemas & business logic
βββ π£οΈ Routes β API endpoint definitions
βββ π§ Middleware β Authentication, validation, error handling
βββ βοΈ Utils β Helpers, pagination, logging
βββ ποΈ Config β Database, environment, app settings
Method
Endpoint
Description
GET
/api/v1/customers
List all customers with pagination & filters
GET
/api/v1/customers/:id
Get specific customer details
GET
/api/v1/customers/stats
Customer analytics & statistics
POST
/api/v1/customers/search
Advanced customer search
Method
Endpoint
Description
GET
/health
Health check
GET
/api/v1/
API documentation
# Clone and setup
git clone < repository>
cd backend
npm install
# Setup environment
cp .env.example .env
# Edit .env with your MongoDB credentials
# Required variables
MONGODB_URI = mongodb+srv://username:password@cluster.mongodb.net/
DATABASE_NAME = company_db
PORT = 5000
# Optional configurations
NODE_ENV = development
DEFAULT_PAGE_SIZE = 20
MAX_PAGE_SIZE = 100
# Load CSV data into MongoDB
npm run load-data
# Verify data loading
npm run verify-data
# Development mode (with auto-reload)
npm run dev
# Production mode
npm start
# Health check
curl http://localhost:5000/health
# Get customers
curl http://localhost:5000/api/v1/customers
# Get customer details
curl http://localhost:5000/api/v1/customers/123
List Customers with Filters
GET /api/v1/customers? page=1& limit=20& search=john& state=California& gender=M& sort_by=created_at& sort_order=desc
POST /api/v1/customers/search
Content-Type: application/json
{
" query" : " john doe" ,
" filters" : {
" states" : [" California" , " Texas" ],
" age_range" : { " min" : 25, " max" : 45 },
" genders" : [" M" ],
" traffic_sources" : [" Search" , " Email" ]
},
" sort" : { " field" : " created_at" , " order" : " desc" },
" page" : 1,
" limit" : 20
}
Customer Details with Orders
GET /api/v1/customers/123? include_orders=true& orders_limit=5
{
"status" : " success" ,
"message" : " Customers retrieved successfully" ,
"data" : {
"customers" : [... ],
"pagination" : {
"current_page" : 1 ,
"total_pages" : 10 ,
"page_size" : 20 ,
"total_count" : 200 ,
"has_next_page" : true ,
"has_prev_page" : false
}
}
}
{
"status" : " error" ,
"code" : 400 ,
"message" : " Validation Error" ,
"errors" : [
{
"field" : " age" ,
"message" : " Age must be between 0 and 150"
}
],
"timestamp" : " 2025-08-04T10:30:00.000Z"
}
search - Name or email search
state - Customer state/province
city - Customer city
country - Customer country
gender - M, F, Other
traffic_source - Search, Email, Social, Direct, Referral, Paid
min_age / max_age - Age range
sort_by - created_at, first_name, last_name, email, age
sort_order - asc, desc
Pagination Parameters
page - Page number (default: 1)
limit - Items per page (default: 20, max: 100)
{
user_id : Number , // Unique identifier
first_name : String , // Customer first name
last_name : String , // Customer last name
email : String , // Email address
age : Number , // Customer age
gender : String , // M, F, Other
location : {
state : String ,
city : String ,
country : String ,
street_address : String ,
postal_code : String ,
coordinates : {
latitude : Number ,
longitude : Number
}
} ,
traffic_source : String , // Acquisition channel
created_at : Date // Registration date
}
{
order_id : Number , // Unique order identifier
user_id : Number , // Reference to user
status : String , // Order status
created_at : Date , // Order date
shipped_at : Date , // Ship date
delivered_at : Date , // Delivery date
num_of_item : Number , // Item quantity
total_amount : Number // Order value
}
src/
βββ app.js # Express app setup
βββ server.js # Server entry point
βββ config/ # Configuration files
βββ controllers/ # Request handlers
βββ models/ # Database models
βββ routes/ # API routes
βββ middleware/ # Custom middleware
βββ utils/ # Utility functions
Express - Web framework
Mongoose - MongoDB ODM
express-validator - Input validation
helmet - Security headers
cors - Cross-origin resource sharing
compression - Response compression
morgan - HTTP request logging
npm run dev # Development server with auto-reload
npm start # Production server
npm run load-data # Load CSV data to MongoDB
npm run verify-data # Verify data integrity
npm test # Run tests (future)
npm run lint # Code linting (future)
β
Input Validation - express-validator for all endpoints
β
Rate Limiting - Prevent API abuse
β
Security Headers - Helmet.js security middleware
β
CORS Configuration - Controlled cross-origin access
β
Environment Variables - Secure configuration management
β
Error Handling - No sensitive information leakage
π Performance Optimizations
β
Database Indexing - Optimized queries for common searches
β
Pagination - Efficient handling of large datasets
β
Aggregation Pipelines - Complex queries with MongoDB aggregation
β
Connection Pooling - MongoDB connection optimization
β
Response Compression - Gzip compression for all responses
# Test health endpoint
curl http://localhost:5000/health
# Test customer list
curl " http://localhost:5000/api/v1/customers?page=1&limit=5"
# Test customer details
curl http://localhost:5000/api/v1/customers/457
# Test customer stats
curl http://localhost:5000/api/v1/customers/stats
Environment Variables for Production
NODE_ENV = production
MONGODB_URI = mongodb+srv://prod-user:password@prod-cluster.mongodb.net/
PORT = 5000
CORS_ORIGIN = https://yourdomain.com
LOG_LEVEL = warn
PM2 Deployment (Recommended)
# Install PM2
npm install -g pm2
# Start application
pm2 start src/server.js --name " customer-dashboard-api"
# Monitor
pm2 status
pm2 logs customer-dashboard-api