goryef
goryef

Reputation: 1489

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.

Upvotes: 20

Views: 20321

Answers (4)

Numaira Nawaz
Numaira Nawaz

Reputation: 47

const indexRouter = require('./routes/index');
const router1Router = require('./routes/router1');
const router2Router = require('./routes/router2');
const express  = require('express');
const routers = express.Router();

module.exports.appRoutes = (app) => {
  router.use('/', indexRouter);
  router.use('/router1', router1Router);
  router.use('/router2', router2Router);

  app.use('/api/v1/',router);
};

and you can use these routes in you app.js file as

 const { appRoutes } = require('../appRoutes');
    appRoutes(app);

Upvotes: 1

SuleymanSah
SuleymanSah

Reputation: 17858

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.

Upvotes: 21

Mohit Kumar
Mohit Kumar

Reputation: 359

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 !

Upvotes: 16

nopassport1
nopassport1

Reputation: 1944

The way I like to organise it is by having a routes folder in my root directory.

So the folder will be located in:
[name of your project]/routes/.

Within the routes folder, I have a router.js file.
You can call this file whatever you want. Something meaningful.
Like router or routes.

So your router folder structure will look something like this:
[name of your project]/routes/router.js.

Within my router.js file, I have the following declaration:

this is just an example

const express = require('express');
const router = express.Router();
const api = require(../api);

/* GET environments */
router.get('/environments', (req, res) =>
{
    res.json({ environments: api.getSortedEnvironments() });
});

... 

module.exports = router;

The essential parts in the above code are the express and router declarations at the top and the module.exports part at the bottom.

Then in my app.js file, I have the following code:

//at the top
const router = require('./routes/router');

and I use it like so:

app.use('/', router);

This means that any endpoints your users are trying to access will automatically be forwarded to/ handled by the 'router' you declared.

Keeps your app.js file nice and tidy.

Your complete folder structure should look like so:

MyAmazingProject
|-- ...
|-- routes
|   `-- router.js
|-- ...
`-- app.js

Upvotes: 4

Related Questions