Skip to content

Latest commit

 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

User CRUD API

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.


Features

  • 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

Tech Stack

  • Node.js
  • Express.js
  • MongoDB
  • Mongoose
  • dotenv
  • Nodemon

Project Structure

user-crud-api/
│
├── models/
│   └── User.js
│
├── routes/
│   └── userRoutes.js
│
├── .env
├── server.js
├── package.json
└── README.md

Installation and Setup

1. Clone the Repository

git clone https://github.com/your-username/user-crud-api.git
cd user-crud-api

2. Install Dependencies

npm install

3. Create .env File

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

4. Run the Project

For development:

npm run dev

For production:

npm start

After running the project, you should see:

MongoDB connected successfully
Server running on port 5000

API Base URL

http://localhost:5000/api/users

API Endpoints

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

User Model

Field Type Required Description
name String Yes User full name
email String Yes User email address
age Number Yes User age
city String Yes User city

Request and Response Examples

Create User

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


Get All Users

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


Get Single User

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


Update User

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


Delete User

Method: DELETE

URL:

http://localhost:5000/api/users/user_id_here

Success Response:

{
  "success": true,
  "message": "User deleted successfully"
}

Status Code: 200 OK


Validation Rules

Field Validation
name Required, minimum 2 characters
email Required, unique, valid email format
age Required, must be greater than 0
city Required

Validation Error Example

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


Duplicate Email Error Example

If the same email already exists:

{
  "success": false,
  "message": "Email already exists"
}

Status Code: 409 Conflict


Status Codes

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

Testing with Postman

You can test this API using Postman.

Steps

  1. Open Postman.
  2. Select the HTTP method.
  3. Enter the API URL.
  4. For POST and PUT requests, go to the Body tab.
  5. Select raw.
  6. Select JSON.
  7. Add the request body.
  8. Click Send.

Example request body:

{
  "name": "John Shepard",
  "email": "john@example.com",
  "age": 24,
  "city": "London"
}

Useful Commands

Install dependencies:

npm install

Run development server:

npm run dev

Run production server:

npm start

Future Improvements

  • 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

Author

Vikas Kushwaha


License

This project is open-source and available for learning and development purposes.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages