Closed toonpang closed 2 years ago
🤔
=>
if (
Array.isArray(excludedRoutes) &&
(isRouteExcluded(excludedRoutes, path, method) ||
// or ['*', '/*', '(.*)', '/(.*)'].includes(path)
['*', '/*', '(.*)', '/(.*)'].includes(path))
)
do you mind giving a bit more explanation on your reply? thanks!
Using .forRoutes(AppController, HealthCheckController)
instead of .forRoutes('*')
works.
Thanks @yn4v4s for you reply. Yup it do work if we add in the controllers manually. However, having a .forRoutes('*')
will be much more convenient as there is no need to add in every time a new controller is created.
Would you like to create a PR to address this issue?
Thanks @yn4v4s for you reply. Yup it do work if we add in the controllers manually. However, having a
.forRoutes('*')
will be much more convenient as there is no need to add in every time a new controller is created.
As an alternative (if your middleware doesn't need any dependencies) you can use a functional middleware and bind it globally in your main.ts using app.use(customMiddleware)
Would you like to create a PR to address this issue?
let me give it a try
@toonpang have you made any progress?
by the way - this also appears to get the job done .forRoutes(AppController, '*')
, where in my case AppController
is the controller for my health check route and excluded from the global prefix. This way you don't need to add every new controller to this list, only those which are explicitly excluded.
Let's track this here https://github.com/nestjs/nest/pull/9332
Hey @kamilmysliwiec, I think this one #9332 introduced a new bug. Considering the middleware in all routes (including the excluded from the prefix) causes parallel consequences like applying the middleware when using @nestjs/serve-static
. Now everyone that access my website gets 401.
In middlewareConsumer.forRoutes()
, exist a way to consider using the api prefix as well? Because here we can not just .forRoutes('api/controller/(.*)')
, only .forRoutes('controller/(.*)')
. Probably may solve all the related problems
嘿@kamilmysliwiec,我认为这个#9332引入了一个新错误。考虑所有路由中的中间件(包括从前缀中排除的)会导致并行结果,例如在使用时应用中间件
@nestjs/serve-static
。现在每个访问我网站的人都会收到 401。在 中
middlewareConsumer.forRoutes()
,是否也存在一种考虑使用 api 前缀的方法?因为在这里我们不能只是.forRoutes('api/controller/(.*)')
,只能.forRoutes('controller/(.*)')
。大概可以解决所有相关的问题
This problem still exists.
Is there an existing issue for this?
Current behavior
Middlewares are not applied to routes (that are excluded by setGlobalPrefix) when using Middlewares with app.setGlobalPrefix.
Minimum reproduction code
https://stackblitz.com/edit/nestjs-typescript-starter-nf5fjs?file=src%2Fmain.ts
Steps to reproduce
https://nestjs-typescript-starter-nf5fjs--3000.local.webcontainer.io/api/test
and######################## THIS IS CUSTOM MIDDLEWARE
is printed out in the consolehttps://nestjs-typescript-starter-nf5fjs--3000.local.webcontainer.io/health-check
and######################## THIS IS CUSTOM MIDDLEWARE
is NOT printed out in the consoleExpected behavior
The middleware should be applied to all routes, including those excluded by global prefix.
Package
Other package
No response
NestJS version
8.2.6
Packages versions
Node.js version
16.14.0
In which operating systems have you tested?
Other
No response