How to fix Error: listen EADDRINUSE while using nodejs

Posted 赏月斋

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了How to fix Error: listen EADDRINUSE while using nodejs相关的知识,希望对你有一定的参考价值。

If I run a server with the port 80, and I try to use xmlHTTPrequest i get this error: Error: listen EADDRINUSE

Why is it problem for nodejs, if I want to do a request, while I run a server on the port 80? For the webbrowsers it is not a problem: I can surf on the internet, while the server is running.

The server is:

  net.createServer(function (socket) {
    socket.name = socket.remoteAddress + ":" + socket.remotePort;
    console.log(‘connection request from: ‘ + socket.remoteAddress);
    socket.destroy();
  }).listen(options.port);

And the request:

var xhr = new XMLHttpRequest();

xhr.onreadystatechange = function() {
    sys.puts("State: " + this.readyState);

    if (this.readyState == 4) {
        sys.puts("Complete.
Body length: " + this.responseText.length);
        sys.puts("Body:
" + this.responseText);
    }
};

xhr.open("GET", "http://mywebsite.com");
xhr.send();


EADDRINUSE means that the port number which listen() tries to bind the server to is already in use.

So, in your case, there must be running a server on port 80 already.

If you have another webserver running on this port you have to put node.js behind that server and proxy it through it.

You should check for the listening event like this, to see if the server is really listening:

var http=require(‘http‘);

var server=http.createServer(function(req,res){
    res.end(‘test‘);
});

server.on(‘listening‘,function(){
    console.log(‘ok, server is running‘);
});

server.listen(80);


解决方案:
What really helped for me was:

killall -9 node
But this will kill a system process.

With

ps ax
you can check if it worked.

  



以上是关于How to fix Error: listen EADDRINUSE while using nodejs的主要内容,如果未能解决你的问题,请参考以下文章

How to Fix Error Code 0x800700ea in Windows 10

How to fix Cannot change version of project facet Dynamic Web Module to 3.0 Error in Eclipse---转载(示例

How to fix use the cURL to connect to GitHub with a 443 HTTPS error All In One

How to Fix Slow Code in Ruby

How to Fix Slow Code in Ruby

How to fix ERR_SSL_VERSION_INTERFERENCE on Chrome?