Running multiple instances of node.js servers on different ports

10,364 views
Skip to first unread message

Arman Didandeh

unread,
Feb 10, 2014, 1:48:34 PM2/10/14
to nod...@googlegroups.com
Dear all,

I am a newbie when it comes to node.js, so I need your help towards running multiple instances of my node.js server on my machine.

I do not want to use other tools that manage this and prefer to do the confing to learn more.

Also  I do not need to assign each of these instances to a core (as I have seen is very popular in discussion forums).

All I need is for my local machine (which is acting as the server during the development phase), to be able to listen to different ports. This way myself and other developers can test their changes in code through these connections and once everything is stable, each person could commit to our git repo. Also the same machine is working as the actual server for current users so it is probably using port 80.

Thanks in advance for your helps.

Cheers all,
Arman

Mark Hahn

unread,
Feb 10, 2014, 1:58:12 PM2/10/14
to nodejs
It should be as simple as running a different node executable for each server.  In other words you'd do 

node server1.js
node server2.js
node server3.js

where each executable is using a different port.  You might want to make the port an argument so you could say `node server.js 8080`.


--
--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com
To unsubscribe from this group, send email to
nodejs+un...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en
 
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Arman Didandeh

unread,
Feb 10, 2014, 2:00:21 PM2/10/14
to nod...@googlegroups.com
So there is no way to run one executable that listens to different ports?


You received this message because you are subscribed to a topic in the Google Groups "nodejs" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/nodejs/FOkOcKQ68Ew/unsubscribe.
To unsubscribe from this group and all its topics, send an email to nodejs+un...@googlegroups.com.

Mark Hahn

unread,
Feb 10, 2014, 2:21:43 PM2/10/14
to nodejs
Interesting question.  One would think than you could put the http server example in the code multiple times with a different variable for each httpserver.  I don't see any reason why that wouldn't work.  Should only take a few minutes to try it.

Arman Didandeh

unread,
Feb 10, 2014, 2:27:58 PM2/10/14
to nodejs
Let me describe the situation a bit more.

So what I have right now is a repo, the code for which has been written by someone else. Now I have myself and other developers who cloned the code on our machines to extend it.

Since each developer is working on different modules, I do not want us to commit or branch on the repo and have the repo run on my server. In fact, I want a separate stable thing to run, and I was planning for each developer to run and test their added codes on their own machines. However some machines are down in RAM, so I trying to deploy a similar connection for the DB over the network (with different users and even possibly different DB clones), but this time on different ports.

If this is what you pointed out, could you please forward me to some instruction, as I am not very good at these things YET!

Cheers

Mark Hahn

unread,
Feb 10, 2014, 2:57:10 PM2/10/14
to nodejs
I'm sorry but I'm getting lost at this sentence ...

I trying to deploy a similar connection for the DB over the network 

You haven't mentioned a DB before.  A node server is a very different thing than a DB server.

Maybe someone else can understand better?  I might not be thinking straight today.

Arman Didandeh

unread,
Feb 10, 2014, 3:02:02 PM2/10/14
to nodejs

My bad.

Right now I have connections to db for each developer on the same server, so everyone can connect. However there is only one db instance.

What I am saying is that is there a way to do the same for the application server? So users can get served by the same server but on different ports. Is it even possible?

Mark Hahn

unread,
Feb 10, 2014, 3:28:53 PM2/10/14
to nodejs
I may be oversimplifying your request but if you mean can one node executable take http requests from multiple ports then the answer is yes, if you use a reverse proxy.  You could set up nginx to accept requests on different ports and proxy them all to one node server.  There might be an easier way that I can't think of right now.

I'm confused as to why this would be useful.  Why can't your users all go to one port?  There will be absolutely no difference at the http level.

Arman Didandeh

unread,
Feb 10, 2014, 3:33:23 PM2/10/14
to nodejs

Cause as I said, each user is a developer who is trying to run his specific veraion of the code, locating on his machine.

Not sure if it makes any sense? As I said, since I am newbie, I might ask silly questions.

Mark Hahn

unread,
Feb 10, 2014, 3:39:58 PM2/10/14
to nodejs
 each user is a developer who is trying to run his specific version of the code, locating on his machine.

This custom code, is it a server or a client?  If it is server code then are you asking for the user's machine to accept requests on multiple ports?

We are going in circles with this discussion.  Can you give more details about the nature of the code?  What code is running on each machine, including any central server.  What does the code do?

Arman Didandeh

unread,
Feb 10, 2014, 3:47:09 PM2/10/14
to nodejs

There is one stable version of code on the server, runing on 80.

The same code is on multiple machines as the base code for further development. Hence testing the new components need the server. This service can come from each machine, on each a node.js server running. This is not a choice for me now.

Another is for each one of those under development codes to be served on the server, each on a specific port.

Since codes are in javascript plus sone python/R, I have both client and server side codes.

Mark Hahn

unread,
Feb 10, 2014, 5:43:19 PM2/10/14
to nodejs
Then may I propose my original response again?  Just run a bunch of different node instances on the server, one for each developer with a different port on each.  I guess i'm not capable of understanding the problem.  Sorry.

Arman Didandeh

unread,
Feb 10, 2014, 5:47:23 PM2/10/14
to nod...@googlegroups.com
I still think that your suggestion might work properly.
Do you have instructions on how to do that?

Jeremy Darling

unread,
Feb 10, 2014, 5:52:18 PM2/10/14
to nodejs
Hopefully this is easy enough to understand, but just setup a configuration option and allow overriding through something like commander.  As an example I've included a server.js file and package.json file to accompany it below (so you can npm install it easily).  Then you can just call it with:

node server
-> Starts on port 8080

node server -p 80
-> Starts on port 80

etc...

Is that what your looking for, if so, here is a quick and dirty sample:

package.json
==========

{
  "name": "wserv",
  "version": "0.0.1",
  "description": "A light weight static webserver for testing pages and shims locally.",
  "main": "server.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "Jeremy Darling <jeremy....@gmail.com>",
  "license": "MIT",
  "dependencies": {
    "connect": "~2.12.0",
    "commander": "~2.1.0"
  }
}




server.js
======

var
    path = require('path'),
    settings = require('commander'),
    connect = require('connect'),
    http = require('http'),
    server = connect(),
    pjson = require(__dirname+'/package.json'),
    cjson = (function(){
      try{
        return require(path.resolve('./cocode.json'))
      }catch(e){
        return {};
      }
    })(),
    defaults = pjson.defaults||{},
    webroot = path.resolve('./'),
    webport = cjson.port||pjson.port||8080
    ;

settings
  .version('v'+pjson.version, '-v, --version')
  .option('-p, --port <int>', 'Set port to serve on, default '+webport, parseInt)
  .option('-f, --folder <int>', 'Set directory to serve from, default '+webroot)
  ;

settings.unknownOption = (function(){
  var cb = settings.unknownOption;
  return function(opt){
    switch(opt){
      case('-H'):
      case('-?'):
        settings.help();
        break;
      default:
        settings.outputHelp();
        cb(opt);
    }
  };
})();

settings.parse(process.argv);

if(settings.port && !isNaN(settings.port)){
  webport = settings.port
}

if(settings.folder){
  webroot = path.resolve('./', settings.folder);
}

server
  .use(connect.favicon())
  .use(connect.directory(webroot))
  .use(connect.static(webroot))
  .use(connect.logger('dev'))
  ;

http
  .createServer(server)
  .listen(webport, function(){
    console.log('Server running on port '+webport);
    console.log('Serving content from '+webroot);
  })
  ;

Mark Hahn

unread,
Feb 10, 2014, 5:53:50 PM2/10/14
to nodejs
Assuming each developer has shell access to the server they could each run node from their github directory in their home directory.  You could have a config file in each home directory that just contains the port number assigned to them.  Then the node server code would read that file when it starts to know what port to use.

Arman Didandeh

unread,
Feb 10, 2014, 5:57:36 PM2/10/14
to nodejs
Thanks to you both.

I will try this and let you know if it worked.

Jeremy Darling

unread,
Feb 10, 2014, 6:11:11 PM2/10/14
to nodejs
Just in case it would help, I posted the source and registered to npm the code I posted before:

Tim Caswell

unread,
Feb 10, 2014, 7:11:10 PM2/10/14
to nod...@googlegroups.com

I'm not sure it's what you want, but yes, a single node process can listen on any number of ports.

You can even have the same exact request handler function service all those ports.

    var http = require('http');
    http.createServer(onRequest).listen(3000);
    http.createServer(onRequest).listen(3001);

    function on request(req, res) { ... }

--

Leonardo Rossi

unread,
Feb 11, 2014, 4:30:47 AM2/11/14
to nod...@googlegroups.com
Hi,

What I've done before is create aliases for network interfaces and let different version of the node code (in different paths) listen on one particular alias address rather than the "official" one.
You can see how to create aliases here:


Hope this helps

Arman Didandeh

unread,
Feb 11, 2014, 12:44:28 PM2/11/14
to nodejs
Thanks to you all.


--
--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com
To unsubscribe from this group, send email to
nodejs+un...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en
 
---
You received this message because you are subscribed to a topic in the Google Groups "nodejs" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/nodejs/FOkOcKQ68Ew/unsubscribe.
To unsubscribe from this group and all its topics, send an email to nodejs+un...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages