A simple RESTful API built with Node.js, Express.js, Mongoose, and MongoDB.
This project provides CRUD operations for a User resource. It can be used as a starter guide for developers who want to build REST APIs using Express and MongoDB.
- Create a new user
- Get all users
- Get a single user by ID
- Update user details
- Delete a user
- Validate required input fields
- Handle duplicate email errors
- Return proper HTTP status codes and messages
- Test API endpoints using Postman
- Node.js
- Express.js
- MongoDB
- Mongoose
- dotenv
- Nodemon
user-crud-api/
│
├── models/
│ └── User.js
│
├── routes/
│ └── userRoutes.js
│
├── .env
├── server.js
├── package.json
└── README.md
git clone https://github.com/your-username/user-crud-api.git
cd user-crud-api
npm install
Create a .env file in the root folder and add the following:
PORT=5000
MONGO_URI=mongodb://127.0.0.1:27017/user_crud_db
For MongoDB Atlas, replace MONGO_URI with your Atlas connection string.
Example:
PORT=5000
MONGO_URI=mongodb+srv://username:password@cluster.mongodb.net/user_crud_db
For development:
npm run dev
For production:
npm start
After running the project, you should see:
MongoDB connected successfully
Server running on port 5000
http://localhost:5000/api/users
| Method | Endpoint | Description |
|---|---|---|
| POST | /api/users |
Create a new user |
| GET | /api/users |
Get all users |
| GET | /api/users/:id |
Get a single user |
| PUT | /api/users/:id |
Update a user |
| DELETE | /api/users/:id |
Delete a user |
| Field | Type | Required | Description |
|---|---|---|---|
| name | String | Yes | User full name |
| String | Yes | User email address | |
| age | Number | Yes | User age |
| city | String | Yes | User city |
Method: POST
URL:
http://localhost:5000/api/users
Body:
{
"name": "John Shepard",
"email": "john@example.com",
"age": 24,
"city": "London"
}
Success Response:
{
"success": true,
"message": "User created successfully",
"data": {
"_id": "user_id_here",
"name": "John Shepard",
"email": "john@example.com",
"age": 24,
"city": "London",
"createdAt": "2026-05-09T10:00:00.000Z",
"updatedAt": "2026-05-09T10:00:00.000Z"
}
}
Status Code: 201 Created
Method: GET
URL:
http://localhost:5000/api/users
Success Response:
{
"success": true,
"message": "Users fetched successfully",
"count": 1,
"data": [
{
"_id": "user_id_here",
"name": "John Shepard",
"email": "john@example.com",
"age": 24,
"city": "London"
}
]
}
Status Code: 200 OK
Method: GET
URL:
http://localhost:5000/api/users/user_id_here
Success Response:
{
"success": true,
"message": "User fetched successfully",
"data": {
"_id": "user_id_here",
"name": "John Shepard",
"email": "john@example.com",
"age": 24,
"city": "London"
}
}
Status Code: 200 OK
User Not Found Response:
{
"success": false,
"message": "User not found"
}
Status Code: 404 Not Found
Method: PUT
URL:
http://localhost:5000/api/users/user_id_here
Body:
{
"name": "John Updated",
"age": 25,
"city": "Manchester"
}
Success Response:
{
"success": true,
"message": "User updated successfully",
"data": {
"_id": "user_id_here",
"name": "John Updated",
"email": "john@example.com",
"age": 25,
"city": "Manchester"
}
}
Status Code: 200 OK
Method: DELETE
URL:
http://localhost:5000/api/users/user_id_here
Success Response:
{
"success": true,
"message": "User deleted successfully"
}
Status Code: 200 OK
| Field | Validation |
|---|---|
| name | Required, minimum 2 characters |
| Required, unique, valid email format | |
| age | Required, must be greater than 0 |
| city | Required |
If required fields are missing:
{}
Response:
{
"success": false,
"message": "Validation failed",
"errors": [
"Name is required",
"Email is required",
"Age is required",
"City is required"
]
}
Status Code: 400 Bad Request
If the same email already exists:
{
"success": false,
"message": "Email already exists"
}
Status Code: 409 Conflict
| Status Code | Meaning |
|---|---|
| 200 | Request successful |
| 201 | Resource created successfully |
| 400 | Bad request or validation error |
| 404 | Resource not found |
| 409 | Duplicate data conflict |
| 500 | Internal server error |
You can test this API using Postman.
- Open Postman.
- Select the HTTP method.
- Enter the API URL.
- For
POSTandPUTrequests, go to the Body tab. - Select raw.
- Select JSON.
- Add the request body.
- Click Send.
Example request body:
{
"name": "John Shepard",
"email": "john@example.com",
"age": 24,
"city": "London"
}
Install dependencies:
npm install
Run development server:
npm run dev
Run production server:
npm start
- Add user authentication using JWT
- Add password hashing using bcrypt
- Add pagination
- Add search and filter options
- Add role-based access control
- Add API documentation using Swagger
- Add unit testing
Vikas Kushwaha
This project is open-source and available for learning and development purposes.