Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to organize routes in Nodejs Express app

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.

like image 526
goryef Avatar asked Jan 10 '20 12:01

goryef


People also ask

How can we create Chainable route handlers for a route path in Expressjs app?

Answer: A is the correct option. By using app. route() method, we can create chainable route handlers for a route path in Express.

How do routes work 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.


2 Answers

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.

like image 191
SuleymanSah Avatar answered Sep 24 '22 15:09

SuleymanSah


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 !

like image 34
Mohit Kumar Avatar answered Sep 20 '22 15:09

Mohit Kumar