【发布时间】:2019-07-20 06:00:03
【问题描述】:
我使用express 和vhost 在同一个端口上设置多个服务器,每个服务器都有不同的子域。每个服务器对应于我的文件系统上的一个本地目录。他们只需要提供静态文件。
~/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/" }
]
运行node ~/repos/server/app.js 后,网址http://localhost:9000/repo1/index.html 在所有3 种浏览器中都可以使用,但http://repo1.localhost:9000/index.html 仅在Chrome 中有效,而在Firefox 或Safari 中无效。
我的代码有问题,还是我需要更改一些浏览器设置?
【问题讨论】:
-
更新:我找到了这个,但不确定它是否有帮助:support.mozilla.org/en-US/questions/1011327
标签: javascript node.js express vhosts