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?