Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why and when should I use express.Router()?

Question 1: Why should I use express.Router()?

The documentation says routers are good for:

  • organizing routes into separate files, thus promoting modularity
  • building router specific middleware
  • essentially creating a mini express app

Cannot, however, seem to find any other reasons to move these routes into smaller router units. Wondering if there's a performance reason or something else I'm not considering here.

Question 2: When should I use express.Router() ?

For example, I have about 10 routes for an express app, and the routes can certainly be categorized into different routers (widgets, users, etc).

Does it make sense to use express.Router() in this app? Trying to gauge if express.Router is used for much bigger projects, where there are dozens (and dozens) of routes. With 10 routes, it still feels somewhat manageable without breaking into separate routers.

like image 650
craft Avatar asked Feb 08 '18 05:02

craft


People also ask

What is the difference between Express () and Express router ()?

The main difference is that express() is a top level function, which means it performs core functionality for the library and it contains its own methods where, as a matter of fact, Router is one, and that is why when we create a specific router we chain the Router() method on express , kind of like how we use app.

What is router method in Express?

Routing refers to how an application's endpoints (URIs) respond to client requests. For an introduction to routing, see Basic routing. You define routing using methods of the Express app object that correspond to HTTP methods; for example, app.get() to handle GET requests and app.post to handle POST requests.

What is the difference between adding Middlewares using router use () and app use ()?

In short, app. use('/first', router) mounts the middleware at path /first, then router. get sets the subpath accordingly.

When should I use Express server?

It is used for designing and building web applications quickly and easily. Web applications are web apps that you can run on a web browser. Since Express. js only requires javascript, it becomes easier for programmers and developers to build web applications and API without any effort.


2 Answers

It's mostly about code organization / maintainability and scoping middleware.

If all you have are ten routes, you're fine without it, especially if they all need the same middleware. At that size it's personal preference, there's no performance advantage or the like.

Once you start having 3-4 or more resources (models) and need routes for each to create,read, update and delete, or if some are authenticated and others are not, etc, you start appreciating the organization more.

like image 38
Paul Avatar answered Oct 18 '22 23:10

Paul


I would use it. The labor cost on your end is minimal and its much cleaner to look at from your own perspective. 10 routes is sufficient to justify separation of concerns. I would do this even if I had a single route, so that when I look at my app.js or main app file, I'm only looking at code that effects the entire system.

Code Size Relevance

The size of the application doesn't matter. Its about not packing unrelated code together. From what you've articulated you have URLs like the list below all being routed from the same file.

  • /accounts
  • /users
  • /invoices
  • /sales
  • /products
  • /shoes
  • /monsters
  • /magic

Thats only 8, now what if you decide to add subdirectories? Having everything packed into one file will make you more likely to cut corners or avoid refactoring and looking at how unrelated the objects above are, do they really all belong in the same file?

Main.js

app.use('/accounts',accountsRoute)
app.use('/users',userRoute)
app.use('/invoices',invoicesRoute)
app.use('/dancers',dancersRoute)
app.use('/monsters',monstersRoute)
app.use('/magic',magicRoute)
like image 131
Usman Mutawakil Avatar answered Oct 19 '22 00:10

Usman Mutawakil