简体   繁体   中英

Can't connect to nodejs server

I run Apache on my server. Going to my address xxxx: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 xxxx:p. )

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

listen(port, 'xxxx')

should just be

listen(port)

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

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

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM