-
Notifications
You must be signed in to change notification settings - Fork 84
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
Continuation control flow from middleware #19
Comments
Hi! Can you expand on your example a little bit :)? It's not clear to me where |
The idea is as simple as trying to continue with the route matching process, instead of running the current matched route (e.g: As far as I can understand, calling Probably my proposal is a sort of workaround to achieve a missing feature like providing a more powerful matching based on multiple request params, such as HTTP headers. E.g: route.use('/users', /* run only if matches the header */ { 'Version': '1.0' }, middleware)
route.use('/users', /* run only if matches the header */ { 'Version': '2.0' }, middleware) |
Gotcha. To me, you are simply describing // this will not run "middleware" if req.headers.version is not 1.0
route.use('/users', onlyIfVersion1, middleware)
route.use('/users', ..) // this is where next('route') jumps you to
function onlyIfVersion1(req, res, next) {
if (req.headers.version !== '1.0') return next('route')
next()
} |
You can find more information about |
Got it! That would be enough. Thanks |
Here is an example of using what is currently in this module that seems like it fits your use-case exactly: route.use('/users', /* run only if matches the header */ requireHeadersMatch({ 'Version': '1.0' }), middleware)
route.use('/users', /* run only if matches the header */ requireHeadersMatch({ 'Version': '2.0' }), middleware)
function requireHeadersMatch(headers) {
return function (req, res, next) {
for (var k in headers) {
if (req.headers[k.toLowerCase()] !== headers[k]) return next('route')
}
next()
}
} |
Cool :) Let me know if you need anything! I was actually just playing around with your |
Thanks for the snippet. My example above was just representative to explain the thing, but the obvios way to do that is via middleware, indeed I created a tiny middleware module which does the same. Regarding to |
Currently the router only discern between path based pattern matching.
In some cases it could be useful to ignore a matched route in the case that during in the middleware dispatch process, providing an extra layer of control flow.
To give you a simple example:
The text was updated successfully, but these errors were encountered: