-
-
Notifications
You must be signed in to change notification settings - Fork 7.6k
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 runs for every matching route #1628
Comments
We could add a validation/matching step that would exclude overlapping routes. I like it! |
This is an expressjs issue, routes RouterProxy will still receive 2 requests. If you do this, then you will have to change all the logic of routes. Without using expressjs middlware. example in expressjs var express = require('express');
var app = express();
app.use('/user/test', function (req, res, next) {
console.log('test method');
next();
});
app.use('/user/:id', function (req, res, next) {
console.log('id method');
next();
});
app.get('/user/:id', function (req, res, next) {
res.send('USER');
});
app.listen(3000)
|
I'm aware of the router behavior in express, but this problem is not particularly about this. |
Can you please tell me where it's at? Where do you want me to look? |
hi // as is
[
{
path: '/cats/test',
method: RequestMethod.GET,
},
{
path: '/cats/:id',
method: RequestMethod.GET,
},
]
/* to be, it is like when it use '/cats' at forRoutes*/
[
{
path: '/cats',
method: RequestMethod.ALL,
}
] |
Current behaviorUse same Expected behaviorI think it should run once,because the same |
@underfin you applied it twice, so it runs twice. |
I have a try for this issue
|
@underfin Do you have an update? I am happy to work on this. |
I pull a pr #3187.If you hava an better idea.Just do it. |
No worries, will find another :) |
@kamilmysliwiec There's a pull request that fixes this issue (#3187) - I'm curious when/if you intend on merging that PR or if there are any changes I can contribute to help get it merge-able? I also experience this issue; it causes some real trickiness when you have a wildcard route at the root, as any middleware intended to apply to that route alone is actually applied to everything. I have a wildcard at the end of my route list that can route to user-defined paths and its middleware is effectively global, unless I maintain a very exhaustive exclude list. |
@kamilmysliwiec Hi, you closed #3187, do you have any other plans to resolve this issue for now? I think it is a rare use case to specify a wildcards ( So for now, I would like to know if there is a workaround for this use case. Do you have any ideas? I have tried to separate the module that handles wildcards from anothers, Before:
After:
but, both |
Yes, it's the expected behavior. This issue refers to a different problem |
Is this still relevant? I'd be happy to take a stab at it, given some directions where to start and what design considerations you have mind. |
@kamilmysliwiec Is this issue still relevant and need a fix? |
It would still be super useful to have this (see many supporters in closed PR #3187)! It looks like the PR was only closed because the proposed solution might lead to side effects, not for a lack of enthusiasm 🎉 |
I want to work on this but I don't understood clearly what needs to be done |
Let's track this here #9809 |
This tests nestjs/nest#1628
Looks closely related to #779
I'm submitting a...
Current behavior
Middleware is being called for every endpoint a request route could potentially match.
Expected behavior
Middleware should only be run once.
Minimal reproduction of the problem with instructions
Using the latest cats sample with the middleware applied from AppModule.
Create "conflicting" routes for the problem to occur
After
curl http://localhost:3000/cats/test
the server outputs:So the middleware is being applied twice, also once for the
:id
endpoint which is unrelated to the current request. The middleware should stop after the first match.Note that if I use
.forRoutes('/cats')
(instead of the controller class), I get the expected behaviour.Environment
The text was updated successfully, but these errors were encountered: