question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

next('route') in middleware does not behave as documented when added with app.use

See original GitHub issue

Hello,

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:closed
  • Created 7 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
dougwilsoncommented, Nov 25, 2016

Ah, thanks for the explanation!

I have found a way to do it, though. I can wrap my router in the subdomain middleware and invoke the router function myself.

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.

0reactions
LaurentVBcommented, Nov 18, 2016

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.

module.exports = (router) => {
  return (req, res, next) => {
    const subdomain = req.subdomains[0];
    if (subdomain) {
      res.locals.subdomain = subdomain;
      router(req, res, next);
    } else {
      next();
    }
  };
};

And I’ll call it this way:

const subdomain = require('./subdomain');
const router = require('./router');
app.use(subdomain(router));

I like it slightly less than app.use(subdomain, router); that I initially though should work, but I can live with it.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found