Is it possible to apply a middleware for all routes (globally)?
See original GitHub issueI’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:
- Created 5 years ago
- Comments:13 (1 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
You can also use
forRoutes({ path: '*', method: RequestMethod.ALL })
: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.
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:
Finally, in the section about DI it starts of with the claim:
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.
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 👍