Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
578 views
in Technique[技术] by (71.8m points)

node.js - is it possible to make nodejs scripts listen to same port

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

I have two scripts

 s1.js, s2.js

both use express framework and listen to certain port.

I call

node s1.js //listens to port 8080

node s2.js //listens to port 8081

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

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

Your comment welcome

Updated question

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

in the path ./app1/index.js

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

in the path ./app2/index.js

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

then I called

  node app

console reports error:

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...