AWS 内部的 express vhost 有一个奇怪的问题。每次部署时,我的 EB 日志中都会出现错误消息:
TypeError: argument hostname is required
at vhost (/var/app/current/node_modules/vhost/index.js:39:11)
at Object.<anonymous> (/var/app/current/app.js:554:9)
at Module._compile (internal/modules/cjs/loader.js:999:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
at Module.load (internal/modules/cjs/loader.js:863:32)
at Function.Module._load (internal/modules/cjs/loader.js:708:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)
at internal/main/run_main_module.js:17:47
/var/app/current/node_modules/vhost/index.js:39
throw new TypeError('argument hostname is required')
^
如果我看一下 vhost 模块 index.js:第 36 行,我们有以下内容:
function vhost(hostname, handle) {
if (!hostname) {
throw new TypeError('argument hostname is required')
}
if (!handle) {
throw new TypeError('argument handle is required')
}
if (typeof handle !== 'function') {
throw new TypeError('argument handle must be a function')
}
甚至没有任何类型检查或主机名上的任何处理,只是检查是否传入了值。它显然在以下代码中:
const app = express();
const register_app = express();
const nonadmin_app = express();
register_app.use(express.static(path.resolve(__dirname, './build/register')));
nonadmin_app.use(express.static(path.resolve(__dirname, './build/nonadmin')));
app.use(vhost('register.<eb-dev-url>.elasticbeanstalk.com/', register_app))
app.use(vhost('nonadmin.<eb-dev-url>.elasticbeanstalk.com/', nonadmin_app))
app.use(vhost('api.<eb-dev-url>.elasticbeanstalk.com/', api))
register_app.get('/register', (req, res) => {
res.sendFile(path.resolve(__dirname, './build/register', 'index.html'));
})
nonadmin_app.get('/nonadmin', (req, res) => {
res.sendFile(path.resolve(__dirname, './build/nonadmin', 'index.html'));
})
我不相信这是 vhost 的问题,因为当使用register.localhost
,nonadmin.localhost
或api.localhost
使用 nodemon 在本地运行此应用程序时,它工作得很好。我也尝试使用.localhost
后缀进行部署,但仍然无法正常工作。
在 AWS 主机名配置方面我缺少什么吗?