A simple Express.js backend project that demonstrates how to split routes into separate modules using controllers and route files.
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
- Node.js
- Express.js
- Nodemon
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.mdClone the repository
git clone https://github.com/a2rp/express-project-08-multiple-route-modules-api.git
cd express-project-08-multiple-route-modules-apiInstall dependencies
npm installStart with nodemon
npm run devStart normally
npm startServer runs at
http://localhost:1198GET /Response
{
"message": "Multiple Route Modules API is running"
}GET /users
GET /users/:id{
"message": "All users fetched successfully",
"data": [
{ "id": 1, "name": "User 1" },
{ "id": 2, "name": "User 2" }
]
}{
"message": "Single user fetched successfully",
"data": {
"id": "1",
"name": "User 1"
}
}GET /products
GET /products/:id{
"message": "All products fetched successfully",
"data": [
{ "id": 1, "name": "Product 1", "price": 499 },
{ "id": 2, "name": "Product 2", "price": 999 }
]
}{
"message": "Single product fetched successfully",
"data": {
"id": "2",
"name": "Product 2",
"price": 799
}
}"scripts": {
"start": "node index.js",
"dev": "nodemon index.js"
}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
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
.restfile
Ashish Ranjan
Follow me
- GitHub: https://github.com/a2rp
- Portfolio: https://www.ashishranjan.net
- LinkedIn: https://www.linkedin.com/in/aashishranjan
- Facebook: https://www.facebook.com/theash.ashish/