req.host / hostname confusion makes vhost behave incorrectly!

Although this has already been dealt with in #2179 , the conversation has been limited to collaborators AND it is slated for a 5.0 release. I think this may warrant a more immediate fix in the 4.x branch?

For a server setup where multiple products (domains) are going to run from the same express server, my use case is this:

In a local machine

In a local machine, we have 2 environments, dev that is a developer environment and staging, that replicates a staging environment. Since there can only be one localhost hostname in a dev environment, we run 2 different server.jss. Either use the vhost module to correctly pickup the correct route.

In a production machine

This won’t matter as we’ll use different hostnames anyway!

Due to the way req.host is structured, I get:

app.use(vhost(localhost:8000), routes1)
app.use(vhost(localhost:3000), routes2)

window.fetch('http://localhost:8000/api/hello')
// Does not hit either!
// Because req.host is just localhost

So in Express 4.x I can never get the desired functionality!

Right now I can see Express has both req.host and req.hostname and they both strip the port.

  1. Can we change either to the desired behaviour of stripping the ports?
  2. I understand this would be a “breaking change”.
    • a. Can we bump up the Middle version number for this?
    • b. If not, can we add some sort of configuration (say using app.set) which would enable this behaviour while keeping it default for 5.x ?

Author: Fantashit

1 thought on “req.host / hostname confusion makes vhost behave incorrectly!

Comments are closed.