question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is it possible to apply a middleware for all routes (globally)?

See original GitHub issue

I’d like to apply a middleware for all routes, but apparently the forRoutes() config is needed. Is there a way to apply the middleware for all routes in the app.module ?

My use case is the JWT authentication for a REST Api which should be active for all endpoints.

export class AppModule {
  configure(consumer: MiddlewaresConsumer) {
    consumer.apply(AuthMiddleware);
  }
}

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:13 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
Mesko89commented, Apr 5, 2018

You can also use forRoutes({ path: '*', method: RequestMethod.ALL }):

export class AppModule {
  configure(consumer: MiddlewaresConsumer): void {
    consumer
      .apply([AuthMiddleware])
      .with('AppModule')
      .forRoutes({ path: '*', method: RequestMethod.ALL });
  }
}
3reactions
felixhayashicommented, Jul 21, 2019

Hi @kamilmysliwiec,

thanks for your reply.

The section regarding global middlewares does not include a note about that only functional middlewares are possible on a “global” level.

2019-07-21 08:40:55

I would recommend to add: “Accessing the DI container in a global middleware is not possible, you need to use a functional middleware instead when using app.use();”

The section about functional middlewares also does not conatin such a hint. Maybe it could be extended to:

Consider using the simpler functional middleware alternative any time your middleware doesn’t need any dependencies or if you need to register the middleware as global middleware.

Finally, in the section about DI it starts of with the claim:

Nest middleware fully supports Dependency Injection.

Which does not seem to be true for global middlewares.

The only part I found where it is implicitly communicated, that DI is impossible for global DI is this part in the section about DI.

Just as with providers and controllers, they are able to inject dependencies that are available within the same module. As usual, this is done through the constructor

May I ask, whether you ever considered also making the services, that are publicly exposed by the different modules via providers, available to global middlewares via DI?

Anyhow, the .forRoutes('*') technique to listen to all requests works fine for me now.

Offtopic: thanks for the great framwork, it is great to see somebody being dedicated to the idea of improving the architecture of nodejs backends 👍

Read more comments on GitHub >

github_iconTop Results From Across the Web

Middleware | NestJS - A progressive Node.js framework
Nest is a framework for building efficient, scalable Node.js server-side applications. It uses progressive JavaScript, is built with TypeScript and combines ...
Read more >
Middleware - Laravel - The PHP Framework For Web Artisans
The withoutMiddleware method can only remove route middleware and does not apply to global middleware. Middleware Groups. Sometimes you may want to group ......
Read more >
Using middleware - Express.js
Express is a routing and middleware web framework that has minimal functionality of its own: An Express application is essentially a series of...
Read more >
Middleware on all routes except one - Laracasts
The above applies the middleware 'exists' to every route request. Is there a way I can make it apply to every route request...
Read more >
Complete Guide to Express Middleware - Reflectoring
For applying the middleware function to all routes, we will attach the function to the app object that represents the express() function. Since ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found