-
-
Notifications
You must be signed in to change notification settings - Fork 7.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Middleware not applied when using setGlobalPrefix #11572
Comments
I also encountered this problem. Has this problem solved? |
@linkFly6 this GH Issue is still open, so no. I didn't investigate it further yet. Feel free to do so. |
Please provide a minimum reproduction repository (Git repository/StackBlitz/CodeSandbox project). |
@kamilmysliwiec it's in the PR description. Is it not minimal enough? |
Description != minimum reproduction repository |
I meant the following: Minimum reproduction code: https://github.com/mentos1386/nestjs-observability/tree/e84c0b92be9416c0d29255007f3a107f6d0d900c Steps to reproduce:
|
Can you provide a minimum(!!) reproduction repository? (see instructions under this link but long story short, no extra libs, irrelevant dependencies, Nx, etc) |
Not sure if this will help any. I did notice a change where I had excluded routes that contained the globalPrefix i/e |
I can confirm the bug is happening in both Nest 9.x and 10.x Here's a minimum reproduction repo: https://stackblitz.com/edit/nestjs-11572?file=src%2Fmain.ts Steps to reproduce:
|
After some investigation, I believe that the issue comes from how the middleware path is being generated from the route info object. Take a look at this test: // packages/core/test/middleware/route-info-path-extractor.spec.ts
Reflect.set(routeInfoPathExtractor, 'prefixPath', '/api');
expect(
routeInfoPathExtractor.extractPathsFrom({
path: '*',
method: RequestMethod.ALL,
}),
).to.eql(['/api/*']); The test is saying: if the global prefix is However, this pattern does not match the root route It would be simple enough to update |
This may be a bug I wrote, I will try to fix it ASAP |
For the path {
method: RequestMethod.GET,
path: '/manager/files/:fileId',
version: '1',
} Would love to contribute to the Nest documentation, if possible! |
Let's track this here #13337 |
It would be great if you could reopen and revisit this, it's clearly not fixed (and it's not just me who confirms this), at least not the part that the MikroORM integration needs. I can confirm the original PR (#12179) does help, unlike the work you ended up doing in #13337. |
The original PR would cause a significant breaking change and cannot be merged as it would lead to major regressions elsewhere. I'll reopen this issue and if someone is willing to create a PR that fixes this specific use-case (and doesn't break anything else), I'd love to merge it as soon as I can. |
How was it breaking? I recall there were no tests broken by it, so how can someone tell if it's breaking or not without it? |
It changed the default request method from "use" to "all" which knowing how different HTTP drivers work, would evidently affect many existing projects. |
I don't know what that means really, I hope someone who actually uses nest and knows the internals can proceed with this, I am unfortunately not the right person. I don't mind solving code puzzles, but I would have to have some tests to show me what I cannot do - to show whether the behavior is breaking or not. It's really hard to expect any kind of "correct PR" to come from outside without that. Or were the changes in tests what you consider breaking? As to me they look sane and expected (and they kinda confirm what the issue is about IMO). |
I mentioned in #13401 (comment) that this error was caused by another PR. I haven't thought of a good solution for now. |
Perhaps adding the same filtering logic to ExpressAdapter as in FastifyAdapter could solve the issue of middleware being called multiple times. nest/packages/platform-fastify/adapters/fastify-adapter.ts Lines 575 to 584 in 3272606
Then we can revert #11832 to fix this issue. I'm not sure if doing this will cause other issues. |
We also had an issue where routes excluded from our global prefix with regex also get excluded from our middleware app.setGlobalPrefix('api', { exclude: ['rest/(.*)'] }); // middleware stops working for rest/* routes This is all due to a bug in the path.replace logic of [email protected] Unfortunately NestJS 10 depends on Express 4, which depends on [email protected] and there are other breaking changes in the 1.x version that prevent just overriding the dependency. Either path-to-regexp would need to backport a patch to their 0.1.x branch or Nest needs to use Express 5. The Nest 11 branch also resolves this issue. Not sure if there are other changes on the Nest side that would resolve this in Nest 10 In the meantime a modification to our exclude regex somehow seems to be working app.setGlobalPrefix('api', { exclude: ['rest/([^]*)'] }); |
Is there an existing issue for this?
Current behavior
I have an app which uses
setGlobalPrefix('api')
and then i have a module (non-root) which sets a middlewareforRoutes('*')
.I would expect the middleware to act on all routes. But it doesn't.
Minimum reproduction code
https://github.com/mentos1386/nestjs-observability/tree/e84c0b92be9416c0d29255007f3a107f6d0d900c
Steps to reproduce
npm ci
npx nx serve example
curl http://localhost:3000/api
example/src/main.ts
and comment out line17
.curl http://localhost:3000
Expected behavior
I would expect the middleware to act on all routes. But it doesn't.
Package
@nestjs/common
@nestjs/core
@nestjs/microservices
@nestjs/platform-express
@nestjs/platform-fastify
@nestjs/platform-socket.io
@nestjs/platform-ws
@nestjs/testing
@nestjs/websockets
Other package
No response
NestJS version
9.4.0
Packages versions
Node.js version
20.0.0
In which operating systems have you tested?
Other
No response
The text was updated successfully, but these errors were encountered: