A scalable and secure backend boilerplate for building Node.js/Express applications. This setup includes authentication, MongoDB integration, and middleware for a production-ready backend API.
Express.js Framework: Lightweight and extensible. MongoDB Integration: Pre-configured with Mongoose for schema-based data modeling. JWT Authentication: Includes ready-to-use register and login endpoints with JWT-based authentication. Validation Middleware: Input validation using express-validator. Error Handling: Centralized error-handling middleware for clean and consistent responses. Secure Routes: Middleware (protect) to guard protected routes.
npx degit KarthikrajS/node-boilerplate my-backend
cd my-backend
npm install
Create a .env file in the project root with the following variables:
MONGO_URI=mongodb://localhost:27017/my-database
JWT_SECRET=your_jwt_secret
PORT=5000
npm start
The server will run at http://localhost:5000 by default.
backend/
├── controllers/ # Logic for handling requests
├── middleware/ # Custom middleware like authentication
├── models/ # MongoDB schemas and models
├── routes/ # API route definitions
├── utils/ # Utility functions (e.g., database connection)
├── .env # Environment variables
└── index.js # Entry point of the application
Method | Endpoint | Description | Auth Required
POST | /register | Register a new user | No
POST | /login | Login and retrieve a token | No
Method | Endpoint | Description | Auth Required
GET | /protected| Example of a route | Yes
Add additional models for your application in models/. Extend the API routes in routes/ with new features. Improve error handling and logging based on your project needs.
Feel free to fork this repository and make your contributions. Open a pull request to submit changes.
This project is licensed under the MIT License.