- Project Overview
- System Architecture
- Technology Stack
- System Components
- Data Models & Relationships
- API Design
- Security Architecture
- Request Flow
- Middleware Pipeline
- Deployment Architecture
- Scalability Considerations
DevCamper API is a RESTful backend API built with Node.js and Express.js for managing an educational bootcamp platform. The system allows users to:
- Manage Bootcamps: Create, read, update, and delete bootcamp listings
- Manage Courses: Associate courses with bootcamps
- User Reviews: Allow users to review and rate bootcamps
- User Authentication: JWT-based authentication with role-based access control
- Geospatial Search: Find bootcamps by location and radius
- File Upload: Upload photos for bootcamps
- RESTful API design
- JWT/Cookie-based authentication
- Role-based access control (User, Publisher, Admin)
- Geocoding integration for location-based searches
- Advanced querying (pagination, filtering, sorting)
- Comprehensive security measures
- File upload capabilities
- Email notifications for password reset
┌─────────────────────────────────────────────────────────────┐
│ Client Layer │
│ (Web App, Mobile App, Postman) │
└───────────────────────────┬─────────────────────────────────┘
│
│ HTTP/HTTPS
│
┌───────────────────────────▼─────────────────────────────────┐
│ Application Layer │
│ ┌──────────────────────────────────────────────────────┐ │
│ │ Express.js Server │ │
│ │ ┌──────────────┐ ┌──────────────┐ ┌───────────┐ │ │
│ │ │ Middleware │→ │ Routes │→ │Controller │ │ │
│ │ │ Pipeline │ │ Handler │ │ Logic │ │ │
│ │ └──────────────┘ └──────────────┘ └─────┬───────┘ │ │
│ └────────────────────────────────────────────┼─────────┘ │
│ │ │
│ ┌────────────────────────────────────────────▼─────────┐ │
│ │ Business Logic Layer │ │
│ │ • Authentication & Authorization │ │
│ │ • Validation │ │
│ │ • Geocoding │ │
│ │ • File Processing │ │
│ │ • Email Service │ │
│ └──────────────────────────────────────────────────────┘ │
└───────────────────────────┬─────────────────────────────────┘
│
│
┌───────────────────────────▼─────────────────────────────────┐
│ Data Layer │
│ ┌──────────────────────────────────────────────────────┐ │
│ │ MongoDB Database │ │
│ │ • Users Collection │ │
│ │ • Bootcamps Collection │ │
│ │ • Courses Collection │ │
│ │ • Reviews Collection │ │
│ └──────────────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────────┘
┌─────────────────────────────────────────────────────────────┐
│ External Services │
│ • Geocoding API (MapQuest/Google) │
│ • Email Service (Nodemailer) │
│ • File Storage (Local Filesystem) │
└─────────────────────────────────────────────────────────────┘
- Layered Architecture: Separation of concerns with distinct layers (Routes → Controllers → Models)
- MVC Pattern: Model-View-Controller pattern (though View is replaced by JSON responses)
- Middleware Pattern: Request processing through middleware pipeline
- Runtime: Node.js (v20.15.0+)
- Framework: Express.js (v5.2.1)
- Database: MongoDB with Mongoose ODM (v8.9.5)
- Language: JavaScript (ES6+)
helmet: Security headersexpress-rate-limit: Rate limiting (100 req/10min)hpp: HTTP Parameter Pollution protectioncors: Cross-Origin Resource Sharinglusca: CSRF protectionbcryptjs: Password hashingjsonwebtoken: JWT token generation/verification
dotenv: Environment variable managementmorgan: HTTP request loggerexpress-fileupload: File upload handlingnode-geocoder: Geocoding service integrationnodemailer: Email sendingslugify: URL-friendly string generationjoi: Input validation
nodemon: Development server auto-reloadjest: Testing frameworksupertest: HTTP assertion libraryprettier: Code formatting
Defines API endpoints and maps them to controller functions.
Structure:
routes/
├── bootcampsRoute.js # /api/v1/bootcamps
├── coursesRoute.js # /api/v1/courses
├── authRoute.js # /api/v1/auth
├── usersRoute.js # /api/v1/users
└── reviewsRoute.js # /api/v1/reviews
Responsibilities:
- Define HTTP methods (GET, POST, PUT, DELETE)
- Apply middleware (authentication, authorization, validation)
- Route requests to appropriate controllers
Contains business logic for handling requests.
Structure:
controllers/
├── bootcampsController.js # Bootcamp CRUD operations
├── coursesController.js # Course management
├── authController.js # Authentication logic
├── usersController.js # User management
└── reviewsController.js # Review management
Responsibilities:
- Process request data
- Interact with models/database
- Handle business rules
- Return JSON responses
Mongoose schemas defining data structure and validation.
Structure:
models/
├── BootcampModel.js # Bootcamp schema
├── CourseModel.js # Course schema
├── ReviewModel.js # Review schema
└── UserModel.js # User schema
Features:
- Schema validation
- Pre/post hooks (e.g., password hashing, geocoding)
- Virtual fields
- Indexes for performance
Custom middleware functions for request processing.
Key Middleware:
auth.js: JWT authentication and role authorizationerror.js: Global error handleradvancedResults.js: Pagination, filtering, sortingvalidate.js: Request validationlogger.js: Request loggingasync.js: Async error wrapper
Helper functions and services.
Components:
geocoder.js: Geocoding service wrappersendMail.js: Email serviceErrorResponse.js: Custom error classvalidators/: Joi validation schemas
Database connection and configuration.
Components:
db.js: MongoDB connection logic- Handles test/production database switching
┌─────────────┐
│ User │
│─────────────│
│ _id │
│ name │
│ email │◄─────┐
│ password │ │
│ role │ │
│ resetToken │ │
└─────────────┘ │
│
│ (1:N)
│
┌─────────────┐ │ ┌─────────────┐
│ Bootcamp │ │ │ Review │
│─────────────│ │ │─────────────│
│ _id │ │ │ _id │
│ name │ │ │ title │
│ description │ │ │ text │
│ address │ │ │ rating │
│ location │ │ │ bootcamp │──┐
│ careers[] │ │ │ user │──┘
│ user │──────┘ └─────────────┘
│ photo │
│ slug │
└──────┬──────┘
│
│ (1:N)
│
┌──────▼──────┐
│ Course │
│─────────────│
│ _id │
│ title │
│ description │
│ weeks │
│ tuition │
│ bootcamp │──────┐
└─────────────┘ │
│
│ (references)
│
┌──────┴──────┐
│ Bootcamp │
└─────────────┘
{
name: String (required),
email: String (required, unique),
password: String (required, hashed),
role: Enum ['user', 'publisher'] (default: 'user'),
resetPasswordToken: String,
resetPasswordExpire: Date
}Features:
- Password hashing via bcrypt (pre-save hook)
- JWT token generation method
- Password reset token generation
- Password comparison method
{
name: String (required, max 100 chars),
slug: String (auto-generated),
description: String (required, max 500 chars),
website: String (URL validation),
phone: String (max 20 chars),
email: String,
address: String (required),
location: {
type: 'Point',
coordinates: [longitude, latitude],
formattedAddress: String,
street, city, state, zipcode, country
},
careers: [String] (enum: Web Dev, Mobile Dev, UI/UX, etc.),
averageRating: Number (1-10),
averageCost: Number,
photo: String,
housing: Boolean,
jobAssistance: Boolean,
jobGuarantee: Boolean,
acceptGi: Boolean
}Features:
- Auto-generates slug from name (pre-save hook)
- Geocodes address to coordinates (pre-save hook)
- 2dsphere index on location for geospatial queries
- Virtual field for courses (reverse populate)
- Cascade delete courses on bootcamp deletion
{
title: String (required),
description: String (required),
weeks: String (required),
tuition: Number (required),
minimumSkill: Enum ['beginner', 'intermediate', 'advanced'],
scholarshipAvailable: Boolean,
bootcamp: ObjectId (ref: Bootcamp, required)
}Features:
- References Bootcamp model
- Cascade delete when bootcamp is deleted
{
title: String (required),
text: String (required),
rating: Number (required, 1-10),
bootcamp: ObjectId (ref: Bootcamp, required),
user: ObjectId (ref: User, required)
}Features:
- One review per user per bootcamp (unique constraint)
- References both Bootcamp and User models
All endpoints follow RESTful conventions:
- Base URL:
/api/v1 - Resource-based URLs
- HTTP methods: GET, POST, PUT, DELETE
- JSON request/response format
POST /register # User registration
POST /login # User login
GET /logout # User logout
GET /me # Get current user
POST /forgotpassword # Request password reset
PUT /resetpassword/:resettoken # Reset password
PUT /updatedetails # Update user details
PUT /updatepassword # Update password
GET / # Get all bootcamps (paginated, filtered)
GET /:id # Get single bootcamp
POST / # Create bootcamp (auth required, publisher/admin)
PUT /:id # Update bootcamp (owner/admin)
DELETE /:id # Delete bootcamp (owner/admin)
PUT /:id/photo # Upload bootcamp photo (owner/admin)
GET /radius/:zipcode/:distance # Get bootcamps within radius
Query Parameters:
page: Page numberlimit: Results per pageselect: Fields to includesort: Sort fieldfilter: Filter criteria (e.g.,careers[in]=Web Development)
GET / # Get all courses
GET /bootcamp/:bootcampId # Get courses for bootcamp
GET /:id # Get single course
POST /bootcamp/:bootcampId # Create course (auth, publisher/admin)
PUT /:id # Update course (owner/admin)
DELETE /:id # Delete course (owner/admin)
GET / # Get all reviews
GET /bootcamp/:bootcampId # Get reviews for bootcamp
GET /:id # Get single review
POST /bootcamp/:bootcampId # Create review (auth, user/admin)
PUT /:id # Update review (owner/admin)
DELETE /:id # Delete review (owner/admin)
GET / # Get all users (admin only)
GET /:id # Get single user (admin only)
POST / # Create user (admin only)
PUT /:id # Update user (admin only)
DELETE /:id # Delete user (admin only)
Success Response:
{
"success": true,
"data": { ... },
"pagination": { ... } // For list endpoints
}Error Response:
{
"success": false,
"error": "Error message",
"source": "controller_name"
}- JWT Tokens: Stateless authentication
- Cookie-based: Alternative token storage
- Token Expiry: Configurable (default: 30 days)
- Password Hashing: bcrypt with salt rounds (10)
- Role-Based Access Control (RBAC):
user: Can create reviewspublisher: Can create/manage bootcamps and coursesadmin: Full access
- Resource Ownership: Users can only modify their own resources
Request → Helmet → CORS → Rate Limit → HPP → CSRF → Body Parser
Helmet: Sets security HTTP headers
- X-Content-Type-Options
- X-Frame-Options
- X-XSS-Protection
- Strict-Transport-Security
Rate Limiting:
- 100 requests per 10 minutes per IP
- Prevents brute force attacks
HPP (HTTP Parameter Pollution):
- Prevents duplicate parameter attacks
CSRF Protection:
- Enabled in production
- Uses Lusca middleware
CORS:
- Configurable origins
- Credentials support
- Joi Validation: Request body/query validation
- Mongoose Validation: Schema-level validation
- Sanitization: Prevents NoSQL injection
- Minimum 6 characters
- Bcrypt hashing (salt rounds: 10)
- Password reset tokens (SHA-256 hashed, 10-minute expiry)
1. Client Request
↓
2. Express Server Receives Request
↓
3. Security Middleware Stack
├─ Helmet (Security Headers)
├─ CORS (Cross-Origin)
├─ Rate Limiter (100/10min)
├─ HPP (Parameter Pollution)
└─ CSRF (Production only)
↓
4. Body Parsing
├─ JSON Parser
└─ Cookie Parser
↓
5. Logging Middleware
├─ Morgan (Development)
└─ Custom Logger
↓
6. File Upload Middleware (if applicable)
↓
7. Route Matching
↓
8. Route-Specific Middleware
├─ Authentication (protect)
├─ Authorization (authorize roles)
└─ Validation (validate input)
↓
9. Controller Function
├─ Business Logic
├─ Database Operations
└─ External API Calls (Geocoding, Email)
↓
10. Response
├─ Success: JSON with data
└─ Error: Error handler middleware
↓
11. Error Handler (if error occurred)
├─ Log Error
├─ Format Error Response
└─ Send Error JSON
↓
12. Client Receives Response
POST /api/v1/bootcamps
Headers: { Authorization: Bearer <token> }
Body: { name, description, address, ... }
1. Rate limiter checks IP
2. CORS validates origin
3. Helmet adds security headers
4. Body parser extracts JSON
5. Route matches /api/v1/bootcamps
6. protect middleware:
- Extracts token from header/cookie
- Verifies JWT
- Loads user into req.user
7. authorize('publisher', 'admin') middleware:
- Checks req.user.role
8. validate middleware:
- Validates request body with Joi
9. creteBootcamp controller:
- Checks if user already has bootcamp
- Creates bootcamp with user ID
- Pre-save hooks:
* Generates slug
* Geocodes address
- Saves to database
- Returns JSON response
10. Response sent to client
1. Trust Proxy Configuration
2. Cookie Parser
3. Logger Middleware
4. Morgan (Development only)
5. File Upload
6. Helmet (Security Headers)
7. Rate Limiter
8. HPP (Parameter Pollution)
9. CORS
10. Session Middleware
11. CSRF Protection (Production only)
12. Static Files
13. Routes
14. Error Handler (Last)- Extracts JWT from Authorization header or cookie
- Verifies token signature
- Loads user from database
- Attaches user to
req.user
- Checks user role against required roles
- Returns 403 if unauthorized
- Pagination:
page,limit - Field selection:
select - Sorting:
sort - Filtering: Query string parsing
- Populates related data
- Centralized error handling
- Formats error responses
- Logs errors for monitoring
- Handles specific error types:
- CastError (Invalid ID)
- ValidationError (Mongoose)
- Duplicate Key (11000)
┌─────────────────────────────────────────┐
│ Vercel Platform │
│ ┌───────────────────────────────────┐ │
│ │ Node.js Runtime │ │
│ │ ┌───────────────────────────────┐ │ │
│ │ │ Express Application │ │ │
│ │ │ (Serverless Functions) │ │ │
│ │ └───────────────────────────────┘ │ │
│ └───────────────────────────────────┘ │
│ │
│ ┌───────────────────────────────────┐ │
│ │ Static File Storage │ │
│ │ (Bootcamp Photos) │ │
│ └───────────────────────────────────┘ │
└───────────────┬─────────────────────────┘
│
│ MongoDB Connection
│
┌───────────────▼─────────────────────────┐
│ MongoDB Atlas (Cloud) │
│ ┌───────────────────────────────────┐ │
│ │ Database Clusters │ │
│ │ • Users Collection │ │
│ │ • Bootcamps Collection │ │
│ │ • Courses Collection │ │
│ │ • Reviews Collection │ │
│ └───────────────────────────────────┘ │
└─────────────────────────────────────────┘
- Development: Local MongoDB, file-based logging
- Test: Separate test database
- Production: MongoDB Atlas, Vercel serverless
config/config.json: Application configuration.env: Environment variables (not committed)vercel.json: Vercel deployment configuration
- File Storage: Local filesystem (not scalable)
- Single Server: No horizontal scaling
- Database: Single MongoDB instance
- No Caching: Every request hits database
- Current: Local filesystem
- Recommended: AWS S3, Cloudinary, or Azure Blob Storage
- Benefits: Scalable, CDN integration, backup
- Redis: Cache frequently accessed data
- Cache Strategies:
- Bootcamp listings (TTL: 5 minutes)
- User sessions
- Geocoding results
- Indexing: Ensure proper indexes on:
- User email (unique)
- Bootcamp location (2dsphere)
- Bootcamp slug
- Review bootcamp/user combination
- Read Replicas: For read-heavy operations
- Connection Pooling: Optimize MongoDB connections
- Multiple Instances: Deploy multiple server instances
- Load Balancer: Distribute traffic
- Session Management: Use Redis for shared sessions
- Per-User Limits: Beyond IP-based limiting
- Tiered Limits: Different limits for user roles
- Distributed Rate Limiting: Redis-based for multi-instance
- APM Tools: Elastic APM, New Relic
- Log Aggregation: ELK Stack, Splunk
- Error Tracking: Sentry, Rollbar
- Metrics: Prometheus, Grafana
- Queue System: Bull, RabbitMQ
- Use Cases:
- Email sending
- Image processing
- Geocoding (async)
- Report generation
- Current:
/api/v1 - Strategy: Maintain multiple versions during transitions
- Pagination: Prevents large dataset retrieval
- Field Selection: Reduces payload size
- Database Indexes: Faster queries
- Virtual Populate: Efficient related data loading
- Response Compression: gzip compression
- Database Query Optimization: Use
explain()to analyze queries - Connection Pooling: Reuse database connections
- Lazy Loading: Load related data only when needed
- Batch Operations: Group multiple operations
- Framework: Jest
- HTTP Testing: Supertest
- Test Files: Located in
tests/directory
- Unit tests for controllers
- Integration tests for routes
- Authentication/authorization tests
- E2E Tests: Full request/response cycle
- Load Testing: Artillery, k6
- Security Testing: OWASP ZAP
- Coverage Reports: Istanbul/nyc
The DevCamper API is a well-structured RESTful API following best practices for security, validation, and error handling. The layered architecture provides clear separation of concerns, making it maintainable and extensible.
✅ Clean architecture and code organization ✅ Comprehensive security measures ✅ Role-based access control ✅ Geospatial search capabilities ✅ Proper error handling ✅ Input validation at multiple layers
🔧 Cloud-based file storage 🔧 Caching layer (Redis) 🔧 Background job processing 🔧 Enhanced monitoring and logging 🔧 Horizontal scaling support 🔧 API documentation (Swagger/OpenAPI)
Document Version: 1.0
Last Updated: 2024
Maintained By: Development Team