2

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

user2787904
  • 517
  • 2
  • 7
  • 18
  • I have a feeling you are using the wrong IP, check you `ifconfig` to make sure you are using the one you think you should. Possible Duplicate: http://stackoverflow.com/questions/6829563/nodejs-error-eaddrnotavail-cannot-assign-requested-address You can also try listening to all interfaces with `0.0.0.0`, if you omit the hostname the default is `127.0.0.1` so you wouldn't be able to connect remotely – Chad Sep 17 '13 at 16:27
  • @Chad I have tried changing the IP to the one listed in ifconfig, but going to that address times out the connection. – user2787904 Sep 17 '13 at 18:29
  • To those who came here via Google: If you've hosted service on Google cloud and facing this issue, first **make sure you've added the port in the exception rules of [cloud firewall](https://console.cloud.google.com/networking/firewalls) as well as in the operating systems firewall** – Atul Aug 10 '16 at 11:03

3 Answers3

3

I have found out the problem. You don't need to specify a host name:

listen(port, 'x.x.x.x')

should just be

listen(port)

otherwise the server will not accept any connection except ones directed at the specified ip.

user2787904
  • 517
  • 2
  • 7
  • 18
  • 1
    If no IP address is given then it will only look for connections made on the loop back address – Chris Jan 06 '17 at 10:21
2

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.

seumasmac
  • 2,174
  • 16
  • 7
  • I get the same error for any port, even ones that are free. The IP is the correct one, everything works fine with Apache. – user2787904 Sep 17 '13 at 15:50
  • As per your own answer, the IP *wasn't* the correct one! You were listening on one and trying to connect on another. I've updated my own answer to allow for any IP. – seumasmac Sep 18 '13 at 11:43
  • Accepted since you did mention it first. – user2787904 Sep 18 '13 at 13:25
  • I also had the error with the correct IP address, changed the IP address to 0.0.0.0 and no error. Though something else is obviously wrong as when I try to access the server from my browser all I get is a timeout. – Chris Jan 06 '17 at 10:22
0

Sounds like the port is locked up and in use..

The following command will give you a list of node processes running.

ps | grep node

To free up that port, stop the process using the following.

kill <processId>
Alec.
  • 5,371
  • 5
  • 34
  • 69