This project is licensed under the MIT License - see the LICENSE file for details.
-
🔐 Built-in security features (
AppSecurity)- CORS configuration
- Helmet security headers
- Rate limiting
- MongoDB sanitization
- XSS protection
- HPP (HTTP Parameter Pollution) protection
-
📝 Advanced logging system (
logger)- Winston logger implementation
- Separate error and combined logs
- Exception tracking
- Formatted console output
-
🚀 Error Handling
- Global error handler (
GlobalErrorHandler) - Custom error classes (
AppError) - Controller error wrapper (
ControllerErrorHandler)
- Global error handler (
-
📦 Database Integration
- MongoDB connection with mongoose
- Connection pooling
- Health checks
- Graceful shutdown
-
⚙️ Environment Configuration
- Separate development and production configs
- Easy-to-modify environment variables
- Node.js (v14 or higher)
- MongoDB
- Clone the repository:
git clone https://github.com/DanyalAnsari/Backend-Express.js-Template.git- Install dependencies:
npm install-
Configure environment variables:
- Copy
config/development.env.exampletoconfig/development.env - Update the variables as needed
- Copy
-
Start the development server:
npm run dev├── app/ # Application core
│ ├── app.js # Express app setup
│ ├── middlewares/ # Application middlewares
│ └── security/ # Security configurations
├── config/ # Environment configurations
├── database/ # Database setup and configuration
├── logs/ # Application logs
├── services/ # Shared services
├── src/ # Source code
│ ├── controllers/ # Route controllers
│ ├── middlewares/ # Route middlewares
│ ├── models/ # Database models
│ └── routes/ # API routes
└── utils/ # Utility functions and helpers
npm run dev- Start the development server with nodemonnpm start- Start the production server
Key environment variables needed to run the application:
MONGO_DB_URI=mongodb://localhost:27017/
MONGO_DB_NAME=your_database
PORT=5000
NODE_ENV=development
JWT_SECRET=your_secret_key
LOG_LEVEL=debugThe application includes a robust error handling system:
- Custom error classes for different HTTP status codes
- Global error handler for consistent error responses
- Controller wrapper for structured responses
- Helmet for security headers
- Rate limiting for API endpoints
- CORS configuration
- MongoDB query sanitization
- XSS protection
- HTTP Parameter Pollution protection
- Winston logger implementation
- Separate log files for errors and combined logs
- Console output in development
- Automatic log rotation
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the ISC License.