2

I'm using a fresh install of Centos 5.5. I have Node installed and working (I'm just using Node -- no apache, or nginx.), but I cannot figure out how to make a simple server on port 80. Node is running and is listening to port 80. I'm just using the demo app:

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

When I visit my IP, it does not work. I obtained my ipaddress using ifconfig. I've tried different ports. So there must be something I am missing.

What do I need to configure on my server to make this work? I would like to do this without installing apache or nginx.

Luke


Edit-- Ok so, I installed nginx and started it up, to see whether or not it is related to node, and I don't see its welcome page. So it definitely has something to do with the server.

Am I retrieving the IP Address correctly by running: ifconfig then reading the inet addr under eth0?

1
  • 1
    Best not to have your server running as root. If you need root to bind to port 80, drop the permissions after any listen() method calls. e.g. add something like this to the end of your script: try { process.setgid(desiredGroupName); process.setuid(desiredUserName); } catch (e) { console.log('will not run as root: ' + e); process.exit(1); }
    – Walf
    Mar 1, 2012 at 12:26

3 Answers 3

5

Are you running it as root? Otherwise, you will not be able to bind to port 80.

What happens if you change the port number to something (free) above 1024?

Also, what happens if you specify the IP address to listen on, as in the example here?

4
  • I'm running "sudo node app.js" to run the app, so I think it's binding properly. I have tried other ports above 1024, but they don't work either, so I believe it has something to do with the server. Same deal when specifying the IP address.
    – Luke Burns
    Jun 8, 2011 at 19:30
  • After installing and running nginx, I don't see its welcome page. So it's not a problem with node.
    – Luke Burns
    Jun 8, 2011 at 20:05
  • There seems nothing wrong with your code! When I run it through node.js only on either localhost (127.0.0.1) or my local servers IP (192.168.x.y) on port 88 I get "Hello World". Both with "lynx 127.0.0.1:88" or with Firefox.
    – Henk
    Jun 8, 2011 at 20:10
  • The little experience I have running a server tells me that there's a lot I could be completely unaware of. I do believe I'm obtaining my IP the correct way.
    – Luke Burns
    Jun 8, 2011 at 20:35
0

As said in my comment the code works (I ran it as root on FreeBSD).

In Linux 127.0.0.1 (aka localhost) is bound to interface "lo". Interface "eth0" should have a different IP-address.
Stop node; turn off nginx (so port 80 is free); put the eth0-address (so not 127.0.0.1) in the above code instead; restart node and try to connect again to port 80 on the other IP-address.

Hint: check with "netstat -ltn" to what IP-address port 80 is bound and try to connect to that address.

1
  • Oops sorry, I was using the eth0-address. Sorry for the confusion. Node binds the address to port 80, so when I run netstat, I just get the same address I configured my node app with.
    – Luke Burns
    Jun 8, 2011 at 21:20
0

don't know if this is still bothering you, however I once ran into kinda the same error on my Debian 6.0: I could connect with a local browser (same machine) using:

links http://127.0.0.1:80

However,

firefox http://[public_ip]:80

from any other computer to the named Debian system resulted in "connection refused". So what I did was to just leave out the ip setting inside my demo code:

//init
var http = require('http');
var port = 8080 ;
var now  = new Date();
// create server
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.write('Hello World - this is node.js\n');
  res.write('Date on server: ' + now.toGMTString());
  res.end('\nbye!');
}).listen(port, "");
console.log('Server running at port: ' + port);

hope that helps, that did it for me. Note that I changed the port setting to 8080 because there was another server running on port 80 already.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .