DEV Community

Dibyojyoti Sanyal
Dibyojyoti Sanyal

Posted on • Originally published at cloudnativemaster.com

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.

AWS Q Developer image

Your AI Code Assistant

Automate your code reviews. Catch bugs before your coworkers. Fix security issues in your code. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

AWS GenAI LIVE!

GenAI LIVE! is a dynamic live-streamed show exploring how AWS and our partners are helping organizations unlock real value with generative AI.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️