A robust backend solution built with Node.js, Express.js, and MongoDB. This RESTful API provides a solid foundation for web and mobile applications.
- RESTful Architecture: Follows REST principles with proper HTTP methods and status codes
- MongoDB Integration: Uses Mongoose ODM for data modeling and database operations
- Authentication System: JWT-based authentication with secure password storage
- Input Validation: Request validation for data integrity
- Error Handling: Comprehensive error handling with appropriate HTTP status codes
- Pagination: Built-in pagination for data-heavy endpoints
- Modular Design: Clean code organization following the MVC pattern
POST /api/users- Register a new userPOST /api/users/login- Authenticate user & get tokenGET /api/users/profile- Get user profile (Protected)PUT /api/users/profile- Update user profile (Protected)GET /api/users- Get all users (Admin only)
GET /api/products- Fetch all productsGET /api/products/:id- Fetch single productPOST /api/products- Create a product (Admin only)PUT /api/products/:id- Update a product (Admin only)DELETE /api/products/:id- Delete a product (Admin only)POST /api/products/:id/reviews- Create product review (Protected)
- Node.js (v14 or higher)
- MongoDB (local or Atlas)
- Clone the repository
git clone https://github.com/yourusername/node-mongodb-rest-api.git
- Install dependencies
npm install
- Set up environment variables
Create a
.envfile in the root directory with the following variables:
NODE_ENV=development
PORT=5000
MONGO_URI=your_mongodb_connection_string
JWT_SECRET=your_jwt_secret
- Run the server
npm run dev
Most endpoints require authentication. To authenticate, obtain a token by logging in:
POST /api/users/login
{
"email": "user@example.com",
"password": "123456"
}
Then use the token in subsequent requests:
Authorization: Bearer your_token_here
backend/
├── config/ # Database configuration
├── controllers/ # Route controllers
├── middleware/ # Custom middleware
├── models/ # Database models
├── routes/ # API routes
├── utils/ # Utility functions
└── server.js # Entry point
This project is licensed under the MIT License - see the LICENSE file for details.