User Management API (Express + MongoDB)
A modular and production-ready REST API built using Node.js, Express.js, and MongoDB. The project follows a clean, layered structure with Controllers, Services, Routes, Middlewares, and Helpers to maintain scalability and readability.
🗂 Project Folder Structure
expressjs-structure/
│
├── controllers/
│ └── user.controller.js # Handles request/response handling
│
├── services/
│ └── user.service.js # Business logic / DB operations
│
├── models/
│ └── user.model.js # Mongoose User schema
│
├── routes/
│ └── user.route.js # User route definitions
│ └── index.js # Registers all routes
│
├── middlewares/
│ └── error-handler.middleware.js
│ └── index.js
│
├── helpers/
│ └── otp.js
│ └── password-hashing.js
│ └── index.js
│
├── constants/
│ └── index.js # Includes API_PREFIX
│
├── http-services/ # (Reserved for external API calls)
│
├── .env.example # Environment config structure
├── index.js # App entry file
├── package.json
└── README.md
Create .env from .env.example:
PORT=5000
MONGODB_URI=mongodb://localhost:27017/your_database
npm install
npm run dev
http://localhost:<PORT>
/api/v1/
| Field | Type | Required | Unique | Description |
|---|---|---|---|---|
| String | Yes | Yes | User email address | |
| name | String | No | No | Full name of the user |
| password | String | No | No | Account password (hashed before save) |
| createdAt | Date | Auto | — | Timestamp of creation |
| updatedAt | Date | Auto | — | Timestamp of last update |
POST /api/v1/user
Request Body
{
"email": "test@example.com",
"name": "John Doe",
"password": "secret123"
}
Response
{
"message": "User created successfully",
"data": {
"_id": "67409a14781b3122af5b6e32",
"email": "test@example.com",
"name": "John Doe"
}
}
GET /api/v1/user
Response:
[
{
"_id": "67409a14781b3122af5b6e32",
"email": "jane@example.com",
"name": "Jane Doe",
"createdAt": "2025-10-30T14:35:11.000Z",
"updatedAt": "2025-10-30T14:35:11.000Z"
}
]
GET /api/v1/user/:id
Example Response:
{
"_id": "67409a14781b3122af5b6e32",
"email": "test@example.com",
"name": "John Doe"
}
PUT /api/v1/user/:id
Request:
{
"name": "Updated Name"
}
Response:
{
"message": "User updated successfully"
}
DELETE /api/v1/user/:id
Response:
{
"message": "User deleted successfully"
}
This project hashes user passwords before storing them in MongoDB using bcrypt. Password hashing prevents plain-text password storage and enhances application security.
bcrypt.hashSync(password, Number(BCRYPT_SALT_ROUNDS));
All controllers are wrapped with:
errorHandler(UserController.method)
This ensures:
Centralized error logging
Clean controller code
Consistent response format