http.createServer on specific IP address

3,160 views
Skip to first unread message

Rob Faraj

unread,
Apr 1, 2011, 12:04:57 AM4/1/11
to nod...@googlegroups.com
I originally posted this to the IRC, but it's crickets in there right now...

I'm trying to run node.js on an interface that is not the primary IP on my server with the following...

var http = require('http');
http.createServer(function (req, res) {
   res.writeHead(200, {'Content-Type': 'text/plain'});
   res.end('Hello World\n');
}).listen(80, "111.22.33.44");

when i try to run this as myself I get 'EACCES, Permission denied'
when i try to run this as root I get 'EADDRINUSE, Address already in use'

however, netstat -ntlp confirms this interface is not listening on port 80 (or any port)
but I do see the interface in ifconfig

any ideas?

Aria Stewart

unread,
Apr 1, 2011, 12:35:23 AM4/1/11
to nod...@googlegroups.com
On Thursday, March 31, 2011 at 10:04 PM, Rob Faraj wrote:

> when i try to run this as myself I get 'EACCES, Permission denied'
> when i try to run this as root I get 'EADDRINUSE, Address already in use'
>
> however, netstat -ntlp confirms this interface is not listening on port 80 (or any port)
> but I do see the interface in ifconfig

Any chance that something is bound to the unspecified interface -- it'll wildcard and block that interface. Look in netstat -lntp for :: or 0.0.0.0 on port 80.

----
Aria Stewart

Rob Faraj

unread,
Apr 1, 2011, 1:31:11 AM4/1/11
to nod...@googlegroups.com
0.0.0.0 is listening on port 80. I have apache running on the primary interface which is different than the one I included in my node script. I thought I could have two different interfaces listen on the same port. Is that not correct? 

Laurie Harper

unread,
Apr 1, 2011, 10:46:40 AM4/1/11
to nod...@googlegroups.com
On 2011-04-01, at 12:04 AM, Rob Faraj wrote:
> [...]

> }).listen(80, "111.22.33.44");
>
> when i try to run this as myself I get 'EACCES, Permission denied'
> when i try to run this as root I get 'EADDRINUSE, Address already in use'

Only root is allowed to bind to ports below 1024, so the first error is expected.

> however, netstat -ntlp confirms this interface is not listening on port 80 (or any port)
> but I do see the interface in ifconfig

On 2011-04-01, at 1:31 AM, Rob Faraj wrote:
> 0.0.0.0 is listening on port 80. I have apache running on the primary interface which is different than the one I included in my node script. I thought I could have two different interfaces listen on the same port. Is that not correct?

0.0.0.0 is a 'pseudo' interface; binding to that means 'listen on this port on all interfaces'. You *can* have different listeners on the same port on different interfaces, but in this case you have two -- one explicitly, and one implicitly via the 0.0.0.0 listener.

It *is* possible to have two listeners on the same port and interface, if the appropriate socket options are set, but in this case what you want to do is have whatever process is binding to 0.0.0.0:80 bind to a specific interface instead.

HTH,
--
Laurie Harper
http://laurie.holoweb.net/

Reply all
Reply to author
Forward
0 new messages