How do you structure a scalable Express js project with multiple route modules

0 votes
tell me via and example and code How do you structure a scalable Express.js project with multiple route modules?
Feb 25 in Node-js by Nidhi
• 10,860 points
48 views

1 answer to this question.

0 votes

Implementation Steps:

Initialize Express App (app.js)

const express = require("express");

const userRoutes = require("./routes/user.routes");

const app = express();

app.use(express.json());

app.use("/api/users", userRoutes); // Use modular routes

module.exports = app;


Create Route Modules (routes/user.routes.js)

const express = require("express");

const { getUsers } = require("../controllers/user.controller");

const router = express.Router();

router.get("/", getUsers);

module.exports = router;


Define Controllers (controllers/user.controller.js)

exports.getUsers = (req, res) => {

    res.json({ message: "List of users" });

};

Start Server (server.js)

const app = require("./app");

const PORT = process.env.PORT || 5000;

app.listen(PORT, () => {

    console.log(`Server running on port ${PORT}`);

});

answered Feb 25 by Navya

Related Questions In Node-js

0 votes
1 answer

How do you log content of a JSON object in Node.js?

Hello @kartik, Try this one: console.log("Session: %j", session); If the ...READ MORE

answered Jul 16, 2020 in Node-js by Niroj
• 82,840 points
956 views
0 votes
1 answer

How do you run a js file using npm scripts?

Hello @kartik, Try this: { "scripts" : { ...READ MORE

answered Oct 12, 2020 in Node-js by Niroj
• 82,840 points
11,683 views
0 votes
1 answer

How to save a stream into multiple destinations with Gulp.js?

Hello @kartik, Currently you have to use two ...READ MORE

answered Oct 14, 2020 in Node-js by Niroj
• 82,840 points
1,532 views
0 votes
1 answer

How do you model a many-to-many relationship in MongoDB with an example?

In MongoDB, a many-to-many relationship can be ...READ MORE

answered Feb 23 in Node-js by Kavya
59 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
2,042 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,570 views
0 votes
1 answer

Unable to start express server on AWS instance

It's not your code — you can't connect ...READ MORE

answered Oct 1, 2018 in AWS by Priyaj
• 58,020 points
3,274 views
0 votes
1 answer

How do I use ES6 features like destructuring in a Node.js application?

To use ES6 features like destructuring in ...READ MORE

answered Dec 17, 2024 in Node-js by Navya
102 views
0 votes
1 answer

How can you preload data for a route in React?

Preloading Data for a Route in React ...READ MORE

answered Feb 24 in Node-js by Kavya
87 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP