Express: Switching multiple router instances at runtime

Created on 28 Mar 2018  路  4Comments  路  Source: expressjs/express

Regarding question #2596 it's possible to switch a router instance during runtime to react to route changes. Especially in a hot module replacement environment this is amazing! Thanks for that hint! It works great!

...but only if a don't work with virtual hosts. In our setup we create multiple router instances. One for every configured virtual host. Than we're using expressjs/vhost middleware to check onto which router we have to "redirect" the request. If routes will be changed during runtime we recreate every router instance like we do it with just one host (one router) but these changes won't be visible in the apps.

Does the router replacement on express side only work with one instance per runtime?

We're on express version 4.16.2.

var routing = function (vhost) {
                return function (req, res, next) {
                    _vhosts[vhost]["router"](req, res, next);
                }
            };
appHttp.use(routing(thisVhost));

_vhosts[vhost]["router"] contains a router instance for every virtual host. If there is no virtual host there will be a "*" entry and therefore only one router instance.

question

Most helpful comment

@wesleytodd Thanks for the hint! I wasn't aware of the fact that I can have multiple Router instances with new express.Router(). I thought that express.Router() is kind of a factory.
Now every vhost gets his own router instance and with hot module replacement I switch instances if there are changes to controllers or routes configs. Works very well!! Many thanks again!

All 4 comments

Hey guys, nobody has an opinion on that? Am I doing something fundamental wrong here or am I missing something?

Would be glad to get an opinion. Thanks guys!

Hey @moecre. Sorry there is a lot of activity in this repo, so sometimes issues slip through the cracks.

What you are doing is close to what I would do. I don't know much about the vhost module, as I have never used it, but it all looks similar to an answer I gave the other day to a similar question. Does this look like what you need? https://github.com/expressjs/express/issues/1765#issuecomment-377987574

I dont think https://github.com/tommymessbauer/express-vhost is being maintained anymore. This one looks like it is. https://github.com/expressjs/vhost

@wesleytodd Thanks for the hint! I wasn't aware of the fact that I can have multiple Router instances with new express.Router(). I thought that express.Router() is kind of a factory.
Now every vhost gets his own router instance and with hot module replacement I switch instances if there are changes to controllers or routes configs. Works very well!! Many thanks again!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

cuni0716 picture cuni0716  路  3Comments

extensionsapp picture extensionsapp  路  3Comments

guyisra picture guyisra  路  3Comments

ZeddYu picture ZeddYu  路  3Comments

zackarychapple picture zackarychapple  路  3Comments