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

标签 node.js express sails.js vhosts cloud-hosting

我正在尝试将我的 Nodejs/Express 托管服务器设置为在我的 VPS 上运行多个应用程序(Sails.js 应用程序类型),但我收到此错误:

/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 基本配置很好,因为它可以很好地与这个 express vhosts 示例配置一起工作:
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, e.g.:
 * `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 (i.e. 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'));
})();

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

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

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

最佳答案

这里的问题是您试图硬塞一个示例,该示例旨在让 Express 应用程序与 Sails 应用程序一起工作。

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

一点肘部润滑脂就可以让它发挥作用。您可以使用 sails.load 来代替提升 Sails 应用程序,它会执行所有操作,除了开始监听端口。这是一种异步方法,因此它还需要重新处理您的 server.js

Sails app.js 文件变为:

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

每个正在运行的 sails 实例将其底层 Express 应用公开为 .hooks.http.app,因此在您的 server.js 中,使用 async或类似加载所有 Sails 应用程序的东西,然后将它们与 vhost Hook :

// 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);

});

关于带有 vhost 的 Node.js/Express 与 Sails.js 框架应用程序冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25345423/

相关文章:

java - 如何从 Angular 2 应用程序执行驻留在服务器上的 Java 代码?

node.js - 为什么 Ajv 在编译期间无法解析引用?

javascript - Mongoose map

node.js - 如何使用 Redis 作为多个 aws 实例或 ELB 的 session 存储?

javascript - 使用 node.js 的 mailchimp-api 插件将用户添加到 mailchimp 邮件列表

mysql - 没有互联网连接,数据库无法连接

node.js - 如何将Nodejs 8.x版本更改为Nodejs 6.x版本?

javascript - 通过ejs模板执行onclick函数

node.js - Puppeteer 不渲染存储在 AWS S3 上的图像

node.js - 如何使用 Sails.js v1 中的 .find() 方法进行不区分大小写的搜索