is it possible to make nodejs scripts listen to same port

Viewed 6640

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
3 Answers
Related