A production-ready RESTful backend API built with Node.js, Express, and MongoDB to manage user data efficiently.
- Full CRUD operations: Create, Read (all/single), Update (full/partial), and Delete.
- Advanced Querying: Built-in support for Pagination, Filtering, and Sorting.
- Global Error Handling: Centralized error responses for consistent API behavior.
- Security: Implements
helmetfor secure HTTP headers. - Logging: Integrated
morganfor HTTP request logging. - CORS Support: Cross-Origin Resource Sharing enabled.
- Node.js: Runtime environment
- Express.js: Web framework
- MongoDB: Database
- Mongoose: ODM for MongoDB
- Helmet: Security enhancement
- Morgan: HTTP request logger
- dotenv: Environment variable management
| Method | Endpoint | Description |
|---|---|---|
| POST | /users |
Create a new user |
| GET | /users |
Get all users (supports pagination, filter, search) |
| GET | /users/:id |
Get a single user by ID |
| PUT | /users/:id |
Update full user details |
| PATCH | /users/:id |
Update specific user fields |
| DELETE | /users/:id |
Delete a user |
- Search:
?search=krishna(Searches in name and email) - Filtering:
?gender=male&age[gte]=20 - Sorting:
?sort=nameor?sort=-createdAt(prefix with-for descending) - Pagination:
?page=1&limit=5
GET /: Health check and basic API info.
{
"name": "Krishna",
"email": "krishna@gmail.com",
"age": 21,
"gender": "male"
}-
Clone the repository:
git clone https://github.com/Prajapati-Krishna18/nodejs-user-management-api.git cd nodejs-user-management-api -
Install dependencies:
npm install
-
Configure Environment Variables: Create a
.envfile in the root directory and add:PORT=5000 MONGO_URI=your_mongodb_connection_string NODE_ENV=development
-
Run the application:
- Production:
npm start - Development:
npm run dev
- Production:
To deploy this API to Render:
- Create a New Web Service: Connect your GitHub repository.
- Configure Build & Start Commands:
- Build Command:
npm install - Start Command:
npm start
- Build Command:
- Set Environment Variables:
MONGO_URI: Your MongoDB Atlas connection string.NODE_ENV:productionPORT:10000(Render default).