DEV Community

Dibyojyoti Sanyal
Dibyojyoti Sanyal

Posted on • Originally published at

How to add multiple routers in a node application without using app.use() for each router ?

In node.js, when we have multiple Express.Router written in a separate file to enhance modularization we need to import each route file into the application server file and add lots of app.use() statements. Now, if we add a new router we need to remember to add that too in the file where the application server is written.

Let's assume there is a routes folder where the routes are defined for user and order handling separately in separate files. in application server file we need to use app.use two times. imagine if our routers increase the solution will not be very good.

we can add a small code snippet in our application server JS file that will automatically import and add all of the routes in the application.
In this blog I gave an example of the initial approach and also showed how that code snippet looks like.

Discussion (0)