1

I’m using express and vhost to set up multiple servers on the same port, each with a different subdomain. Each server corresponds to a local directory on my file system. They only need to serve static files.

~/repos/server/app.js:

const fs = require('fs')
const path = require('path')

const express = require('express')
const vhost   = require('vhost')

const app = express()
const PORT = 9000
const virtual_hosts = require('./virtual-hosts.json') // see below

app.use(express.static(path.join(__dirname, '../')))

virtual_hosts.forEach((vh) => {
    var vh_app = express()
    vh_app.use(express.static(path.join(__dirname, '../', vh.path)))
    app.use(vhost(vh.domain, vh_app))
})

app.listen(PORT, () => {
    console.log(`
        Listening at http://localhost:${PORT}/
        Press ctrl + c to stop.
    `)
    console.log('...')
})

~/repos/server/virtual-hosts.json:

[
    { "domain": "repo1.localhost", "path": "./repo1/" },
    { "domain": "repo2.localhost", "path": "./repo2/" },
    { "domain": "repo3.localhost", "path": "./repo3/" }
]

After running node ~/repos/server/app.js, the url http://localhost:9000/repo1/index.html works in all 3 browsers, but http://repo1.localhost:9000/index.html only works in Chrome, but not Firefox or Safari.

Is there something wrong with my code, or do I need to change some browser settings?

chharvey
  • 8,580
  • 9
  • 56
  • 95
  • update: I found this, but not sure if it will help: https://support.mozilla.org/en-US/questions/1011327 – chharvey Feb 03 '20 at 19:37

1 Answers1

1

It's a little bit late, but for someone who come up with the same question.

You can't use subdomains on windows of "localhost". I dont know how chrome manage to handle it, but the normal behaviour is that you can't do that.

If you still want to archieve this, have a look at this.