I am working on Nodejs/Express app.I used express-generator to create basic structure. For now a specify all routes in the app.js file
var indexRouter = require('./routes/index');
var router1Router = require('./routes/router1');
var router2Router = require('./routes/router2');
......
app.use('/', indexRouter);
app.use('/router1', router1Router);
app.use('/router2', router2Router);
Everything is works as expected. However I came across a suggestion to have routes.js file in the root folder of the application and
Require all routes in this and then require this file in app.js
However, I am having hard time to figure out how to set it up properly. Any suggestions will be greatly appreaciated. Thanks in advance.
Answer: A is the correct option. By using app. route() method, we can create chainable route handlers for a route path in Express.
A route is a section of Express code that associates an HTTP verb ( GET , POST , PUT , DELETE , etc.), a URL path/pattern, and a function that is called to handle that pattern. There are several ways to create routes.
It is a good idea to keep the main file short and simple.
I put all my routes to the routes folder.
|-- app.js
|-- routes
| |-- index.js
| |-- router1.js
| |-- router2.js
|-- startup
| |-- routes.js
In the startup/routes.js file I import all the routes like this:
const express = require("express");
var indexRouter = require("../routes/index");
var router1Router = require("../routes/router1");
var router2Router = require("../routes/router2");
module.exports = function(app) {
app.use(express.json());
app.use("/", indexRouter);
app.use("/router1", router1Router);
app.use("/router2", router2Router);
};
And App.js I only import startup/routes.js like this:
const express = require("express");
const app = express();
require("./startup/routes")(app);
const port = process.env.PORT || 3000;
app.listen(port, () => console.log(`Listening on port ${port}...`));
In this way, when we want to add another route to our app, we add it to the startup/routes.js, without needing to make change to the App.js file, which keeps our App.js clean and short.
You can also add other files (for example database connect) to the startup folder later when required, and import them to the App.js.
Multiple ways to achieve that. This is what I find convenient. You can have a folder for routes and there you can have index.js and files for other routes which you require. Example: File: src/routes/index.js
const express = require('express');
const contactusRoutes = require('./contactus.routes');
const apiRouter = express.Router();
apiRouter.use('/contactus', contactusRoutes);
module.exports = apiRouter;
File : /src/routes/contactus.routes.js
const express = require('express');
const contactusRoutes = express.Router();
const contactusController = require('../controllers/contactus.controller');
contactusRoutes.post('/', errorHandler.wrapAsync(contactusController.someFunctionName));
module.exports = contactusRoutes;
you can have more files like this.
File: server.js/ app.js
add these to server.js/app.js
const apiRoutes = require('./src/routes/index');
server.use('/api', apiRoutes);
Thank you !
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With