Setting up a purely Node.js http server on port 80

http-servernode.jsport-80

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?

Best Answer

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?

Related Topic