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
Calling next('route') in middleware is ignored if a router is next in the chain #2591
Comments
This is correct. Things inside |
To sum up what
If you provide the use-case for what you are trying to achieve, I can certainly point you into the right direction for implementing it. |
I double-checked in the docs (http://expressjs.com/4x/api.html) to see if something in there was mis-leading, but we clearly only document |
Looking at it now, it appears quite obvious this wouldn't work. I can see my misunderstanding came from Anyway, my use case came from trying to build authentication middleware that would have routers either throw an error or continue based on the route that was called, based on a certain condition. The idea was to leave it up to the application route definitions to determine what would happen to each route without relying on state or having the router care about whether or not they've been authenticated |
Ah, I see. If you have any suggestions for improving our documentation, we have a great team over at https://github.com/strongloop/expressjs.com who can take care of it :) Just file an issue/PR with your suggestions. We try to think of everything, but since we are not new users, it's hard to think in their shoes, so getting feedback from people like you on our documentation is always great :) As for the use-case, I'm not sure if I 100% follow, but two different ways to directly translate app.use('/foo', function fn(req, res, next) {
if (/* some condition you would call next('route') */) {
return next();
}
return router(req, res, next);
}); or if you want to keep things encapsulated: app.use('/foo', createFn(router));
function createFn(proceed) {
return function fn(req, res, next) {
if (/* some condition you would call next('route') */) {
return next();
}
return proceed(req, res, next);
};
} |
The following test :
When applied to
test/app.router.js
will fail. Instead of jumping to the next registered '/foo/bar', it ends up calling the mounted '/bar' route anyway.The text was updated successfully, but these errors were encountered: