Skip to content

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.

Notifications You must be signed in to change notification settings

nomanaslam1696/expressjs-setup

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

⚙️ Environment Variables

Create .env from .env.example:

PORT=5000
MONGODB_URI=mongodb://localhost:27017/your_database

📦 Installation & Setup

Install Dependencies

npm install

Run Development Server

npm run dev

Server runs at:

http://localhost:<PORT>

🧱 API Base URL

/api/v1/

👤 User Model

Field Type Required Unique Description
email 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

🔗 API Endpoints

1) Create User

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"
  }
}

2) Get All Users

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"
  }
]

3) Get User By ID

GET /api/v1/user/:id

Example Response:

{
  "_id": "67409a14781b3122af5b6e32",
  "email": "test@example.com",
  "name": "John Doe"
}

4) Update User

PUT /api/v1/user/:id

Request:

{
  "name": "Updated Name"
}

Response:

{
  "message": "User updated successfully"
}

5) Delete User

DELETE /api/v1/user/:id

Response:

{
  "message": "User deleted successfully"
}

🔐 Password Hashing

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));

🧱 Error Handling

All controllers are wrapped with:

errorHandler(UserController.method)

This ensures:

Centralized error logging

Clean controller code

Consistent response format

About

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.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published