Is it common practice to filter routes with middleware?

Question

Is it common practice to filter routes with middleware?

Answer

Depending on the library we are using to set up our server endpoints we will manage our route filtering different. Yet, most of them rely on some sort of middleware.

When we talk about Express, we will always implement one way or another a middleware function or library method to filter our routes.

we can implement the filter through a .use() method:

app.use(['/this/route', 'this/otherroute'], middlewareFunction);

It will run the middleware assigned, anytime there is a request coming from any of the set routes.

We can also attach it through the request method as another parameter:

app.get('this/route', middlewareFunction, (req, res, next) => {...});

this way it will run in any get request on that route.

In conclusion, filtering routes with middleware is common practice with Express.

1 Like