This is a simple RESTful API built using Node.js, Express, and MongoDB
It supports basic CRUD (Create, Read, Update, Delete) operations with proper error handling and JSON responses.
- Create a User – Accepts name, email, and age, then stores them in a database.
- Retrieve All Users – Returns a list of users.
- Retrieve a Single User – Fetches a user by ID.
- Update a User – Updates user details using an ID.
- Delete a User – Removes a user using an ID.
- JSON formatted responses.
- Proper error handling for invalid input, non-existent users, etc.
- Clean and well-commented code structure.
- Node.js
- Express.js
- MongoDB with Mongoose
- JavaScript
- Clone the repository:
git clone [email protected]:Mohit123singh/Backend-development-Intern-Assignment.git- Install dependencies:
npm install- Environment Variables:
Create a .env file in the root directory:
PORT=5000
MONGO_URI=your_mongodb_connection_string
- Run the server:
npm run devThe server will run at:
http://localhost:5000
| Method | Endpoint | Description | Request Body (JSON) |
|---|---|---|---|
| POST | /api/users |
Create a new user | { "name": "Mohit Sngh", "email": "[email protected]", "age": 24 } |
| GET | /api/users |
Retrieve all users | None |
| GET | /api/users/:id |
Retrieve a single user by ID | None |
| PUT | api//users/:id |
Update a user by ID | { "name": "Updated Name", "age": 30 } |
| DELETE | api/users/:id |
Delete a user by ID | None |
Request:
POST /users
Content-Type: application/json
{
"name": "Vishal",
"email": "[email protected]",
"age": 24
}Response:
{
"user": {
"_id": "67e061343fff4df19972f78b",
"name": "Vishal",
"email": "[email protected]",
"age": 24
}
}GET /usersResponse:
[
{
"_id": "67e04c164c72c35f9356a68a",
"name": "Mohit Singh",
"email": "[email protected]",
"age": 24,
},
{
"_id": "67e061343fff4df19972f78b",
"name": "Vishal",
"email": "[email protected]",
"age": 24
}
]GET /users/67e04c164c72c35f9356a68aResponse:
{
"_id": "67e04c164c72c35f9356a68a",
"name": "Mohit Singh",
"email": "[email protected]",
"age": 24
}PUT /users/
Content-Type: application/json
{
"name": "Vishal Updated",
"age": 24
}Response:
{
"user": {
"_id": "67e061343fff4df19972f78b",
"name": "Vishal Updated",
"email": "[email protected]",
"age": 24
}
}DELETE /users/67e061343fff4df19972f78bResponse: (Id of deleted-user)
{
"67e061343fff4df19972f78b"
}- Invalid ID Format:
{
"error": "Invalid User ID format"
}- User Not Found:
{
"error": "User not found"
}- Missing Required Fields:
{
"error": "Name, email, and age are required"
}Mohit Kumar Singh
- All responses are in JSON format.
- Proper error handling is implemented for each route.