Skip to content

A production-ready Express.js backend template with built-in configurations and best practices for API development.

License

Notifications You must be signed in to change notification settings

DanyalAnsari/Backend-Express.js-Template

Repository files navigation

Backend Express.js Template

License

This project is licensed under the MIT License - see the LICENSE file for details.

Features

  • 🔐 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

  • 📦 Database Integration

    • MongoDB connection with mongoose
    • Connection pooling
    • Health checks
    • Graceful shutdown
  • ⚙️ Environment Configuration

    • Separate development and production configs
    • Easy-to-modify environment variables

Prerequisites

  • Node.js (v14 or higher)
  • MongoDB

Installation

  1. Clone the repository:
git clone https://github.com/DanyalAnsari/Backend-Express.js-Template.git
  1. Install dependencies:
npm install
  1. Configure environment variables:

    • Copy config/development.env.example to config/development.env
    • Update the variables as needed
  2. Start the development server:

npm run dev

Project Structure

├── 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

Available Scripts

  • npm run dev - Start the development server with nodemon
  • npm start - Start the production server

Environment Variables

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=debug

Error Handling

The 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

Security Features

  • Helmet for security headers
  • Rate limiting for API endpoints
  • CORS configuration
  • MongoDB query sanitization
  • XSS protection
  • HTTP Parameter Pollution protection

Logging

  • Winston logger implementation
  • Separate log files for errors and combined logs
  • Console output in development
  • Automatic log rotation

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the ISC License.

About

A production-ready Express.js backend template with built-in configurations and best practices for API development.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published