Skip to content

Latest commit

 

History

History
196 lines (145 loc) · 3.12 KB

File metadata and controls

196 lines (145 loc) · 3.12 KB

express-project-08-multiple-route-modules-api

A simple Express.js backend project that demonstrates how to split routes into separate modules using controllers and route files.

Project Goal

This project focuses on learning how to organize an Express.js application in a cleaner way by splitting different routes into separate files.

It covers:

  • multiple route modules
  • separate controllers
  • separate route files
  • modular Express project structure
  • route handling for users and products

Tech Stack

  • Node.js
  • Express.js
  • Nodemon

Project Structure

express-project-08-multiple-route-modules-api
├─ src
│  ├─ controllers
│  │  ├─ user.controller.js
│  │  └─ product.controller.js
│  └─ routes
│     ├─ user.route.js
│     └─ product.route.js
├─ index.js
├─ api-test.rest
├─ .gitignore
├─ package.json
├─ package-lock.json
└─ README.md

Installation

Clone the repository

git clone https://github.com/a2rp/express-project-08-multiple-route-modules-api.git
cd express-project-08-multiple-route-modules-api

Install dependencies

npm install

Run the Project

Start with nodemon

npm run dev

Start normally

npm start

Server runs at

http://localhost:1198

API Endpoints

Root Route

GET /

Response

{
    "message": "Multiple Route Modules API is running"
}

User Routes

GET /users
GET /users/:id

GET /users response

{
    "message": "All users fetched successfully",
    "data": [
        { "id": 1, "name": "User 1" },
        { "id": 2, "name": "User 2" }
    ]
}

GET /users/:id response

{
    "message": "Single user fetched successfully",
    "data": {
        "id": "1",
        "name": "User 1"
    }
}

Product Routes

GET /products
GET /products/:id

GET /products response

{
    "message": "All products fetched successfully",
    "data": [
        { "id": 1, "name": "Product 1", "price": 499 },
        { "id": 2, "name": "Product 2", "price": 999 }
    ]
}

GET /products/:id response

{
    "message": "Single product fetched successfully",
    "data": {
        "id": "2",
        "name": "Product 2",
        "price": 799
    }
}

Available Scripts

"scripts": {
  "start": "node index.js",
  "dev": "nodemon index.js"
}

Testing

This project includes an api-test.rest file for quick API testing in VS Code using the REST Client extension.

Included test routes:

  • GET /
  • GET /users
  • GET /users/1
  • GET /products
  • GET /products/2

Learning Outcome

By building this project, you learn how to:

  • split routes into separate modules
  • create separate controllers for different resources
  • keep Express projects clean and organized
  • connect multiple route files in a single entry file
  • test backend APIs using a .rest file

Author

Ashish Ranjan

Follow me