[nodejs] Can HttpServer listen to multiple ports?

7,962 views
Skip to first unread message

Joson Mandaro

unread,
Aug 21, 2010, 8:23:07 AM8/21/10
to nod...@googlegroups.com
Hi all,
I wanna add many listeners onto a httpserver, new ports and new callback functions. All of these ports receive httpRequest and parse them with HTTPParser. I'm not sure if nodejs supports that?
Regards,
Joson

mscdex

unread,
Aug 21, 2010, 11:04:21 AM8/21/10
to nodejs
You can't listen on multiple ports for a single http.Server instance.

I think the next best thing is to create your callbacks as named
functions and use them with each instance of http.Server you create to
listen on each port you want.

Example:

var http=require('http'),
ports = [8000, 8080, 9000],
servers = [];

function reqHandler(req, res) {
// Do something with the request here
}

ports.forEach(function(port) {
var s = http.createServer(reqHandler);
s.listen(port);
servers.push(s);
});

Sami Samhuri

unread,
Aug 22, 2010, 1:53:04 AM8/22/10
to nodejs
Do you mean something like this?

var servers = [],
port = 8000

function addServer(fn) {
var s = http.createServer(fn)
s.listen(port)
servers.push(s)
port += 1
}

addServer(function(req,resp){ resp.writeHead(200); resp.end('hi') })
addServer(function(req,resp){ resp.writeHead(200);
resp.end('hello') })
addServer(function(req,resp){ resp.writeHead(200);
resp.end('howdy') })

----

% curl localhost:8000
hi
% curl localhost:8001
hello
% curl localhost:8002
howdy

If so then yup, supported.

-s

Joson Mandaro

unread,
Aug 22, 2010, 3:12:16 AM8/22/10
to nod...@googlegroups.com
Thank both of you.
In these two examples, different servers will be created and run in different process. I supposed to append new pair of port-Listener onto a httpserver. So, when request is sent to port2, this server will run listener2 to make a right response.
And then between web logic ( on different port and listener), they can communicate in the same process. it should be faster than communication between multiple process.
Now the nodejs doesn't support multiple ports, I'm trying to implement this kind of server. Or any other solution?

2010/8/22 Sami Samhuri <sami.s...@gmail.com>

-s

--
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.


Михаил Белошицкий

unread,
Aug 22, 2010, 3:31:33 AM8/22/10
to nod...@googlegroups.com
2010/8/22 Joson Mandaro <joson...@gmail.com>

Thank both of you.
In these two examples, different servers will be created and run in different process.

No, it run in the same process.
 
I supposed to append new pair of port-Listener onto a httpserver.

In node.js creating tow httpServer instances works the same way. I think you just need more experiments with nodejs. Get mscdex's exmaple code and replace

function reqHandler(req, res) {
   // Do something with the request here
}

 with 

var sharedVariable = 'Hou-hou-hou';

function reqHandler(req, res) {
  res.writeHead(200, {'Content-Type':'text/plain'})
  res.end(sharedVariable)
}
 
Run  this and you see that servers running in the same process and address space.

Marak Squires

unread,
Aug 22, 2010, 1:50:32 PM8/22/10
to nod...@googlegroups.com
We've got a pretty decent solution for binding multiple http servers to the same port. It involves spawning multiple processes and monkey punching the http module a bit. 

The code isn't open yet, but if anyone needs to setup a similar setup I'd be willing to discuss it a bit. Feel free to send me an email.

-Marak

2010/8/22 Михаил Белошицкий <mbelos...@gmail.com>

joseph moniz

unread,
Aug 22, 2010, 2:34:01 PM8/22/10
to nod...@googlegroups.com
I still see it fairly reasonable to just spawn multiple nodejs
processes behind something like Nginx and make nginx do all the load
balancing magic. If that doesn't amuse you enough you can do some
clever hacks with unix-domain sockets. Now, if only nodejs had a
wrapper to socketpair() ...

Peter Griess

unread,
Aug 22, 2010, 2:50:51 PM8/22/10
to nod...@googlegroups.com
NodeJS does have a wrapper for socketpair(2).

You shouldn't need to use this directly, however, if all you want to do is to run multiple processes listening on the same port. Both http://github.com/senchalabs/spark and http://github.com/kriszyp/multi-node provide scaffolding to easily load balance among multiple processes each listening on the same port.

Peter

Reply all
Reply to author
Forward
0 new messages