express.Router() vs express() in express express.Router() vs express() in express express express

express.Router() vs express() in express


When you are working with a server where there are many routes, it can be confusing to leave them in a Main file together. The let router = express.Router() option works differently than let app = express().

While the app returns an app object, router will return a small app fragment, similar to the app, where you will use logic to call them later on the Main.

The most important, about your question, is that a router, which is isolated, will not interfere with others in the application, being a single environment.

A router object is an isolated instance of middleware and routes. You can think of it as a “mini-application,” capable only of performing middleware and routing functions. Every Express application has a built-in app router.

A router behaves like middleware itself, so you can use it as an argument to app.use() or as the argument to another router’s use() method.