是否可以让 nodejs 脚本监听同一个端口 [英] is it possible to make nodejs scripts listen to same port

查看:165
本文介绍了是否可以让 nodejs 脚本监听同一个端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在另一个帖子里问了同样的问题,但答案误解了我的问题,所以我再问一次

I ask the same question in another thread, but the answer misunderstood my question , thus I ask again

我有两个脚本

 s1.js, s2.js

都使用 express 框架并监听特定端口.

both use express framework and listen to certain port.

我打电话

node s1.js //listens to port 8080

node s2.js //listens to port 8081

启动两个脚本,一个监听8080端口,另一个监听8081端口.

to start the two scripts, one listens to port 8080, another listens to port 8081.

是否可以让 nodejs 脚本监听同一个端口 8080?调用时如何分离两个脚本

is it possible to make nodejs scripts listen to same port 8080? how to separate the two scripts when I call

www.abc.com:8080

www.abc.com:8080

欢迎您的评论

更新问题

我尝试编写 app.js

I try code app.js

  var express = require("express");
  var app = express();

  app.use('/app1', require( './app1/index.js').app);
  app.use('/app2', require( './app2/index.js').app);

  app.listen(8081);

在路径./app1/index.js

in the path ./app1/index.js

  var express = require("express");
  var app = express();
  console.log("app1");
  app.listen(8081);

在路径./app2/index.js

in the path ./app2/index.js

  var express = require("express");
  var app = express();
  console.log("app2");
  app.listen(8081);

然后我打电话给

  node app

控制台报错:

app1

/Users/myname/node_modules/express/lib/application.js:111
  if (fn.handle && fn.set) app = fn;
        ^
TypeError: Cannot read property 'handle' of undefined
    at Function.app.use (/Users/zhengwang/node_modules/express/lib/application.js:111:9)
    at Object.<anonymous> (/Users/zhengwang/multi.js:27:7)
    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:901:3

推荐答案

Cluster 会这样做,Express.vhost 也会这样做.实际上,您可以同时使用这两个端口,并且只能同时使用 1 个端口.

Cluster does this, and so does Express.vhost. You can in fact use both and only use 1 port all together.

// server.js
var cluster = require('cluster');
var express = require('express');
var app = express ();

var cpus = require('os').cpus().length - 1;

if(cluster.isMaster) {
    for(var i=0;i<cpus;i++) {
        cluster.fork ();
    }
}
else {
    app
        .use( express.vhost('www.site1.com'), require('./site1.js').app ) )
        .use( express.vhost('www.site2.com'), require('./site2.js').app ) )
        .listen(8080);
}

在 site1.js、site2.js 中,您可以像编写任何其他节点应用程序一样将它们写出来,除了两件小事.导出您的应用程序 (var app =exports.app = express() ),并且不要在这两个文件中监听 (app.listen(8080)).您可以看到 server.js 已经为您完成了监听,并且还需要从它们中导出两个应用程序".现在,您在同一台服务器上的所有站点都可以在同一端口上运行,并且可以在您的服务器拥有的每个 CPU 上运行.我确实相信您的服务器本身也需要为多个虚拟主机正确设置,但这超出了问题的范围.

In the site1.js, site2.js, you write them out like you'd write any other node app, except for two little things. Export your app (var app = exports.app = express() ), and don't listen (app.listen(8080)) in those two files. You can see the server.js already does the listening for you, and also requires the two 'app' exports from them. Now all your sites on the same server can run on the same port, and on every CPU your server has. I do believe your server itself needs to be setup correctly too, for multiple vhosts, but that's beyond the question asked.

集群处理向正在运行的节点应用程序 (server.js) 之一发送请求,然后根据使用的 url 对请求使用正确的应用程序(site1.js、site2.js)进行表达处理.我想说的很简单.我真的很喜欢节点.

Cluster deals with sending requests to one of the node apps (server.js) running, then express deals with using the right app (site1.js, site2.js) for the request based on the url used. Surprisingly simple I'd say. I do love node.

这篇关于是否可以让 nodejs 脚本监听同一个端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆