Node.js   发布时间:2022-04-24  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了带有vhost的Node.js / Express与Sails.js框架应用程序冲突大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试设置我的Nodejs / Express托管服务器以在我的VPS上运行多个应用程序(Sails.js应用程序类型),但是我收到此@L_262_0@:

/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基本配置适用于多个应用程序,因为它适用于此快速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"
}

@L_262_0@来自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连接它们:

// 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框架应用程序冲突全部内容,希望文章能够帮你解决带有vhost的Node.js / Express与Sails.js框架应用程序冲突所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。