A production-ready Express.js + MongoDB backend template with built-in user management, validation, and API documentation.
- Features
- API Endpoints
- Request/Response Examples
- Setup & Installation
- Project Structure
- Environment Variables
- Error Handling
- Rate Limiting
- API Documentation
- π RESTful API with proper HTTP methods and status codes
- π‘ Input Validation using express-validator
- β‘ Rate Limiting to prevent abuse
- π API Documentation with Swagger UI
- π§ͺ Error Handling with proper error messages
- π Search & Pagination for user listings
- π Soft Delete functionality
- π Environment-based configuration
| Method | Endpoint | Description |
|---|---|---|
GET |
/api/users |
Get all users (paginated) |
GET |
/api/users/:id |
Get a single user by ID |
POST |
/api/users |
Create a new user |
PATCH |
/api/users/:id |
Update a user's details |
DELETE |
/api/users/:id |
Deactivate a user (soft delete) |
DELETE |
/api/users/:id/permanent |
Permanently delete a user |
GET |
/api/users/search?q= |
Search users by name or email |
Request:
GET /api/users?page=1&limit=10Response (200 OK):
{
"data": [
{
"_id": "5f8d0d55b54764421b7156c8",
"name": "John Doe",
"email": "[email protected]",
"isActive": true,
"createdAt": "2023-10-15T08:00:00.000Z",
"updatedAt": "2023-10-15T08:30:00.000Z"
}
],
"meta": {
"total": 1,
"page": 1,
"limit": 10,
"totalPages": 1
}
}Request:
POST /api/users
Content-Type: application/json
{
"name": "Jane Smith",
"email": "[email protected]"
}Response (201 Created):
{
"_id": "5f8d0d55b54764421b7156c9",
"name": "Jane Smith",
"email": "[email protected]",
"isActive": true,
"createdAt": "2023-10-15T09:00:00.000Z",
"updatedAt": "2023-10-15T09:00:00.000Z"
}Request:
PATCH /api/users/5f8d0d55b54764421b7156c9
Content-Type: application/json
{
"name": "Jane Doe"
}Response (200 OK):
{
"_id": "5f8d0d55b54764421b7156c9",
"name": "Jane Doe",
"email": "[email protected]",
"isActive": true,
"createdAt": "2023-10-15T09:00:00.000Z",
"updatedAt": "2023-10-15T10:00:00.000Z"
}- Node.js 14.x or later
- npm 6.x or later
- MongoDB (local or cloud instance)
-
Create a new project using npx:
npx bootnode my-backend
This will:
- Create a new directory called
my-backend - Set up all necessary files and folders
- Install all required dependencies
- Create a new directory called
-
Navigate to your project directory:
cd my-backend -
Configure your environment variables:
cp .env.example .env # Edit .env with your MongoDB connection string and other settings -
Start the development server:
npm run dev
The server will start on
http://localhost:5000by default. -
Access the API documentation at
http://localhost:5000/api-docs
src/
βββ config/ # Configuration files
β βββ db.js # Database connection
β βββ swagger.js # API documentation
βββ controllers/ # Route controllers
β βββ user.controller.js
βββ middleware/ # Custom middleware
β βββ rateLimiter.js
β βββ validators/
β βββ user.validator.js
βββ models/ # Database models
β βββ user.model.js
βββ routes/ # Route definitions
β βββ user.routes.js
βββ app.js # Express application setup
| Variable | Description | Default |
|---|---|---|
PORT |
Server port | 5000 |
MONGODB_URI |
MongoDB connection string | mongodb://localhost:27017/bootnode |
NODE_ENV |
Application environment | development |
RATE_LIMIT_WINDOW_MS |
Rate limiting window in ms | 15 * 60 * 1000 (15 minutes) |
RATE_LIMIT_MAX |
Max requests per window | 100 |
The API returns consistent error responses with appropriate HTTP status codes:
400 Bad Request- Invalid input data404 Not Found- Resource not found409 Conflict- Duplicate resource (e.g., email already exists)429 Too Many Requests- Rate limit exceeded500 Internal Server Error- Server error
Example error response:
{
"success": false,
"message": "Validation error",
"errors": ["Email is required", "Name must be at least 3 characters"]
}The API implements rate limiting to prevent abuse:
- 100 requests per 15 minutes per IP address
- Headers included in responses:
X-RateLimit-Limit: Maximum requests allowedX-RateLimit-Remaining: Remaining requests in windowX-RateLimit-Reset: Timestamp when window resets
Interactive API documentation is available at /api-docs when the server is running. This provides:
- Full endpoint documentation
- Request/response schemas
- The ability to test endpoints directly from the browser
To access the API documentation:
- Start the server
- Open
http://localhost:5000/api-docsin your browser
This project is licensed under the MIT License - see the LICENSE file for details.