next('route') in middleware does not behave as documented when added with app.use
See original GitHub issueHello,
I have been able to reduce this issue to a very simple test case:
const express = require('express');
const app = express();
const bypass = (req, res, next) => {
return next('route');
};
app.use(bypass, (req, res, next) => {
res.send('Should not happen');
});
app.get('/', (req, res, next) => {
res.send('Should happen');
});
app.listen(3000);
The first call to app.use
defines a middleware chain that should be bypassed by the first middleware, according to the documentation in http://expressjs.com/en/4x/api.html#app.use
In this test case, if I replace app.use
by app.(all|get|post|...)
it behaves as expected.
In my use case, though, I’m mounting a router with some subdomain precondition so I would like to write:
app.use(subdomainPrecondition, require('./subdomainRouter'));
I’m using express 4.14.0 on node 6.9.1.
Regards,
Laurent
Issue Analytics
- State:
- Created 7 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Middleware Next Route in Node Express App Not Fired
In the first middleware function, you're calling next with the parameter 'route' . As stated in the documentation, this causes the ...
Read more >Complete Guide to Express Middleware - Reflectoring
When we request a non-existent route, the third error handler is invoked giving us an error message: invalid path . Using Third-Party Middleware....
Read more >Using middleware - Express.js
This example shows a middleware function with no mount path. The function is executed every time the app receives a request. const express...
Read more >ASP.NET Core Middleware | Microsoft Learn
NET Core app sets up a single request delegate that handles all requests. This case doesn't include an actual request pipeline.
Read more >Routing - Laravel - The PHP Framework For Web Artisans
These routes are assigned the web middleware group, which provides features like session state and CSRF ... use App\Http\Controllers\UserController;.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Ah, thanks for the explanation!
Yes, with the design of Express, this is exactly how you’re supposed to do it 😃 You can find this pattern in our own middlewares, for example the
vhost
middleware (https://expressjs.com/en/resources/middleware/vhost.html), which is doing basically what your little example does.Ok, I get it. Thanks for your patience 😃
The problem I’m trying to solve is this: a dynamic subdomain per client, which will load the app (think XXX.slack.com loads XXX’s slack). If no such subdomain exists, then fallback on the regular website and following routes.
I have found a way to do it, though. I can wrap my router in the subdomain middleware and invoke the router function myself.
And I’ll call it this way:
I like it slightly less than
app.use(subdomain, router);
that I initially though should work, but I can live with it.