-
-
Notifications
You must be signed in to change notification settings - Fork 19.4k
Allow to bypass middleware to last layer with next('last') #3770
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
base: master
Are you sure you want to change the base?
Conversation
Hey @BigFax, thanks for the contribution! While interesting, I think we had wanted to move away from the "magic strings". I think the feature you want seems great, so maybe if we could think of an api to achieve this without the magic string Also, I would look at opening this PR against the router project, as it is where this would land in the 5.x branch. https://github.com/pillarjs/router/ Note: I cannot find the specific issue where "magic strings" were discussed. So feel free to shoot me down if others disagree. |
I don't know the discussion either, but I recall the same thing. The main annoying danger is that most people do like That said, I there there is a more general issue with this solution, in that is is not general enough. For example I can 100% forsee a follow up request if this lands that person x needs it to jump to the 2nd to last middleware, not the last one. I was reading your SO question and I think I know a workable solution based on your comments. I don't have a SO account to respond there and don't want to mix that into the conversation in this PR. You're welcome to open a new issue with the full background to explore that if you like. |
I proposed a new "magic string" because it seemed to me to stick with the vision of the project and the normal behavior of the @dougwilson When i was thinking to call |
Right, that's the problem: you're just thinking about your specific layout. Express makes no such requirement and you can easily have multiple handlers that do rendering based on perhaps what type of user was loaded for example. This is the type of thing someone who is already splitting up everything into separate middlewares is going to do, which is the use-case you're presenting. Just as a simple example of two renders at the end of a route: app.get('/thing', [...],
(req, res, next) => { if (!req.user.isAdmin()) return next(); res.render(...) },
(req, res, next) => { res.render(...) }
) Typically the const ifUserIsAdmin = (handler) =>
(req, res, next) => { if (!req.user.isAdmin()) return next() else handler(req, res, next) }
app.get('/thing', [...],
ifUserIsAdmin((req, res, next) => res.render(...) }),
(req, res, next) => res.render(...) }
) The other thing is just a functional issue with the PR itself, which is that it doesn't work at all in the common case of handling a route-specific error handler on the end of your route (because even your rendering handler may throw an error). Example:
|
I got your point. To me, About the PR issue, you are right. One fix could be :
|
Hi, I think we could rid of "magic string" and pass to and in
|
This is a bit of an older issue, but I think an even better api would be something like:
If we move forward with this I think that would be the api which would get my vote. |
@BigFax @dougwilson what's the status on this? Is it still under consideration? I saw it needed tests and would be happy to help with that if appropriate! |
@bpernick I don't know. I didn't follow this subject since a long time. I still think that if magic strings are kept, |
Most of my current work requires building APIs with express and I see how this can be useful to me as a developer. But I believe this idea would lead to bad design choices when implementing it. Double edged sword imo. If at all it is acceptable, then the API makes more sense than the magic string. |
eb10dba
to
340be0f
Compare
It adds a built-in way to skip to the final route of a layers stack (see this closed issue #1662).
Based on my experience, i was thinking it could be interesting to have this simple feature available.
Example :
In case of errors, this can allow to break the middleware cycle and jump to the last middleware of the route directly without the need to put a condition
if (res.locals.err)
in each middleware to jump to the next one, or to call an error-handling middleware withnext(err);
and to manage everything there, as redirection, render, controller call depending of the error, or to usenext('route')
with a route duplication each time... (which are three solutions not always very convenient).(maybe there is already a way to do that ? I didn't find it yet. See this question and my own question)
Let me know your thoughts about it.