A production-ready boilerplate for building scalable Node.js applications with Express.js and MongoDB. Features TypeScript support, comprehensive security measures, and enterprise-level architecture.
- 🔒 Enterprise Security - CSRF protection, rate limiting, helmet security
- 📝 TypeScript Support - Full type safety and modern JavaScript features
- 🎯 MongoDB Integration - Mongoose ODM with connection retry mechanism
- 🚦 Advanced Error Handling - Centralized error handling with logging
- 💾 Caching System - Built-in caching middleware for performance
- 📊 Logging System - Winston logger with daily rotate file
- 🔄 API Response Handler - Standardized API response format
- Node.js >= 16.0.0
- MongoDB instance (local or Atlas)
- npm or yarn package manager
# Clone the repository
git clone https://github.com/Softworks-Studio/mongoexpress-boilerplate.git
# Install dependencies
npm install
# Create environment file
cp .env.example .env
Create a .env
file in the root directory with the following variables:
PORT=3000
MONGO_URL="your-mongodb-connection-string"
NODE_ENV="development"
SECRET="your-secret-key"
DB_MAX_RETRIES=3
DB_RETRY_DELAY_MS=1000
# Development mode
npm run dev
# Production build
npm run build
npm start
src/
├── api/
│ └── v1/
│ ├── controllers/ # Request handlers
│ ├── database/ # Database models and connection
│ ├── helpers/ # Utility functions
│ ├── middlewares/ # Express middlewares
│ ├── routes/ # API routes
│ └── utils/ # Utility modules
├── config/ # Configuration files
└── index.ts # Application entry point
The boilerplate includes a robust MongoDB connection handler with retry mechanism:
startLine: 17
endLine: 38
Comprehensive security features are implemented through the CoreMiddleware:
startLine: 14
endLine: 82
You can modify the application constants in src/config/constants.ts
:
export const CONSTANTS = {
PORT: process.env.PORT || 3000,
PROJECT_NAME: 'mongoexpress-boilerplate',
API_VERSION: 'v1',
API_PREFIX: '/api',
CORS: {
origin: '*',
methods: ['GET', 'POST', 'PUT', 'DELETE'],
},
RATE_LIMIT: {
windowMs: 15 * 60 * 1000, // 15 minutes
max: 100, // Limit each IP to 100 requests per windowMs
},
}
- Create a new controller in
src/api/v1/controllers
- Create a new route file in
src/api/v1/routes
- Add the route to
src/api/v1/routes/index.ts
Example:
// src/api/v1/controllers/UserController.ts
import { Request, Response } from 'express';
import { catchError } from '../helpers/catch/CatchErrorHelper';
import { handleResponse } from '../helpers/response/HandleResponseHelper';
export const getUsers = catchError(async (req: Request, res: Response) => {
// Your logic here
return handleResponse(res, 200, 'Users retrieved successfully', users);
});
Follow the example model structure:
startLine: 1
endLine: 57
- CSRF Protection
- Rate Limiting
- Helmet Security Headers
- MongoDB Sanitization
- XSS Protection
- HPP (HTTP Parameter Pollution)
- Compression
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
Need help? Contact our support team at [email protected]
This project is licensed under the ISC License - see the LICENSE file for details.
- Express.js Team
- Mongoose Team
- All contributors who help improve this boilerplate
Made with ❤️ by Softworks Team