带有vhost的Node.js / Express与Sails.js框架应用程序冲突

我试图设置我的Nodejs / Express托pipe服务器有多个应用程序(Sails.js应用types)在我的VPS上运行,但我得到这个错误:

/srv/data/web/vhosts/default/node_modules/vhost/index.js:78 throw new TypeError('argument server is unsupported') ^ TypeError: argument server is unsupported at createHandle (/srv/data/web/vhosts/default/node_modules/vhost/index.js:78:9) at vhost (/srv/data/web/vhosts/default/node_modules/vhost/index.js:39:16) at Object.<anonymous> (/srv/data/web/vhosts/default/server.js:46:9) at Module._compile (module.js:456:26) at Object.Module._extensions..js (module.js:474:10) at Module.load (module.js:356:32) at Function.Module._load (module.js:312:12) at Function.Module.runMain (module.js:497:10) at startup (node.js:119:16) at node.js:906:3 

当然,我以前安装了所有的依赖项。
我为多个应用程序的Nodejs / Express基本configuration是好的,因为它适用于这个快速虚拟主机示例configuration:
https://github.com/loicsaintroch/express-vhosts

所以在这里我的nodejs服务器应用程序结构:

 .../vhosts/default/server.js package.json /app1 /app.js /app2 /app.js /app3 /app.js 

在这里我的server.js文件基于这个以前的github的例子:

 // Module dependencies var express = require('express'); var vhost = require('vhost'); var app = express(); // vhosts app .use(vhost('app1.com', require('./app1/app.js'))) .listen(8080); 

和package.json文件:

 { "name": "default", "private": true, "version": "0.0.1", "description": "Default git repository for some web applications.", "dependencies": { "express": "^4.2.0", "vhost": "^2.0.0", "forever": "^0.11.1", "static-favicon": "^1.0.0", "ejs": "^1.0.0", "morgan": "^1.0.0", "cookie-parser": "^1.0.1", "body-parser": "^1.0.0", "debug": "^0.7.4" }, "scripts": { "start": "forever start server.js --prod", "debug": "node debug server.js" }, "main": "server.js" } 

错误来自vhost npm包

 /** * Create handle to server. * * @param {function|Server} server * @return {function} * @api private */ function createHandle(server){ if (typeof server === 'function') { // callable servers are the handle return server } else if (typeof server.emit === 'function') { // emit request event on server return function handle(req, res) { server.emit('request', req, res) } } throw new TypeError('argument server is unsupported') } 

在这里确切地说,我认为, vhost软件包与来自sails.js框架app.js响应有问题。 这里是我的Sails.js应用程序中的app.js文件内容:

 /** * app.js * * Use `app.js` to run your app without `sails lift`. * To start the server, run: `node app.js`. * * This is handy in situations where the sails CLI is not relevant or useful. * * For example: * => `node app.js` * => `forever start app.js` * => `node debug app.js` * => `modulus deploy` * => `heroku scale` * * * The same command-line arguments are supported, eg: * `node app.js --silent --port=80 --prod` */ // Ensure a "sails" can be located: (function() { var sails; try { sails = require('sails'); } catch (e) { console.error('To run an app using `node app.js`, you usually need to have a version of `sails` installed in the same directory as your app.'); console.error('To do that, run `npm install sails`'); console.error(''); console.error('Alternatively, if you have sails installed globally (ie you did `npm install -g sails`), you can use `sails lift`.'); console.error('When you run `sails lift`, your app will still use a local `./node_modules/sails` dependency if it exists,'); console.error('but if it doesn\'t, the app will run with the global sails instead!'); return; } // Try to get `rc` dependency var rc; try { rc = require('rc'); } catch (e0) { try { rc = require('sails/node_modules/rc'); } catch (e1) { console.error('Could not find dependency: `rc`.'); console.error('Your `.sailsrc` file(s) will be ignored.'); console.error('To resolve this, run:'); console.error('npm install rc --save'); rc = function () { return {}; }; } } // Start server sails.lift(rc('sails')); })(); 

==============================================

更新:完整的解决scheme示例
作为一个伟大的答案的综合,我写了一个完整的案例研究可在这里https://github.com/migswd/express-sails-vhosts

==============================================

这里的问题是,你正在试图用一个快速应用程序来与Sails应用程序一起工作的例子。

如果从示例vhost应用程序中查看app.js文件,它们都使用module.exports来返回Express应用程序实例。 您发布的Sails应用程序中的app.js显然没有这样的事情; 它根本不输出任何东西。 此外,该文件正在调用sails.lift ,它启动自己的服务器在端口1337上侦听。

一点点的手肘油脂可以得到这个工作。 除了解除Sails应用程序之外,您可以使用sails.load除了在端口上开始侦听之外 ,其他任何事情都可以sails.load 。 这是一个asynchronous的方法,所以它也需要重新修改你的server.js

Sails app.js文件变成:

 var sails = require('sails'); module.exports = function(cb) { process.chdir(__dirname); sails.load(cb); }; 

每个正在运行的.hooks.http.app实例都将其底层Express应用程序公开为.hooks.http.app ,因此在server.js中 ,使用asynchronous或类似的方式来加载所有的Sails应用程序,然后使用vhost连接它们:

 // Module dependencies var express = require('express'); var vhost = require('vhost'); var app = express(); var async = require('async'); async.auto({ app1: require('./app1/app.js'), app2: require('./app2/app.js'), app3: require('./app3/app.js') }, function doneLoadingApps(err, apps) { app .use(vhost('app1.io', apps.app1.hooks.http.app)) .use(vhost('app2.io', apps.app2.hooks.http.app)) .use(vhost('app3.io', apps.app3.hooks.http.app)) // Mix in a vanilla Express app as well .use(vhost('app4.io', require('./app4/app.js'))) .listen(8080); });