【问题标题】:Express Vhost 'argument handle is required'Express Vhost '需要参数句柄'
【发布时间】:2018-09-11 02:46:15
【问题描述】:

首先,在这里找到了几个类似的问题,但没有重复,我认为我的情况略有不同。

尝试使用 vhost for subdomians 让网站和相关 API 在 Express 上运行。

这是我的文件夹结构

/api
  api.js
/server
  website.js
server.js

我的 server.js

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

const app = express();
app.use(vhost('localhost', require('./server/website.js').app));
app.use(vhost('api.localhost', require('./api/api.js').app));

app.listen(1337, () => {});

我的 api.js

const express = require('express');
const app = express();

app.get('/', function(req, res){
    res.send({ hello: 'world' });
});

module.exports = app;

最初我到 api.js 的路径是错误的,我得到了一个未找到的错误,所以现在我知道我的路径是正确的,但现在无论我做什么,我都会收到错误“Typeerror:argument handle is required”。

任何帮助将不胜感激。

【问题讨论】:

    标签: node.js express express-vhost


    【解决方案1】:

    您已经在导出应用程序。所以不需要将 .app 添加到 require 的末尾。

    应该是:

    app.use(vhost('localhost', require('./server/website')));
    app.use(vhost('api.localhost', require('./api/api')));
    

    希望对您有所帮助。

    【讨论】:

      【解决方案2】:

      这就是我所做的:

      //used the api.localhost as the subdomain url
      //it requires another express app.js to work
      //the other express app must -> module.exports = app;
      
      const vhost = require('vhost');
      
      const app = express();
      app.use(vhost('api.localhost', require('./api/app')));
      

      【讨论】:

        猜你喜欢
        • 2021-10-10
        • 1970-01-01
        • 1970-01-01
        • 2013-05-19
        • 1970-01-01
        • 1970-01-01
        • 2022-12-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多