Search code examples
node.js

Can't connect to nodejs server


I run Apache on my server. Going to my address x.x.x.x:port loads the index.html page in /var/www. When I stop the server, I can no longer connect (all good).

Now I start the node server with node server.js (the server.js file below is also located in /var/www).

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

This gives the error listen EADDRNOTAVAIL, but I am not running any other node server (there is no other process running at this port).

I have also tried omitting the IP address and just listening thus: listen(port);

This returns no errors, but I cannot connect to the server (Browser says: Firefox can't establish a connection to the server at x.x.x.x:p.)


Solution

  • The port is in use or not available. Try a different port like:

    listen(88, 'x.x.x.x');
    

    and see if that connects. Also, make sure that x.x.x.x is actually the ip address of your server. You can listen on all IPs by doing:

    listen(88, '0.0.0.0');
    

    or by leaving the host/ip section out entirely. If it does connect on another port, you just need to find what is using the port you want. If it's port 80, use:

    sudo netstat -tulpn | grep :80
    

    to get the program using that port.