How to fix Error: listen EADDRINUSE while using no

2018-12-31 10:22发布

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.\nBody length: " + this.responseText.length);
        sys.puts("Body:\n" + this.responseText);
    }
};

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

30条回答
流年柔荑漫光年
2楼-- · 2018-12-31 10:56

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.

查看更多
何处买醉
3楼-- · 2018-12-31 10:56

Try both commands and it will stop all node process.

killall 9 node
pkill node
npm start 
查看更多
深知你不懂我心
4楼-- · 2018-12-31 10:57

I have the same problem too,and I simply close the terminal and open a new terminal and run

node server.js

again. that works for me, some time just need to wait for a few second till it work again.

But this works only on a developer machine instead of a server console..

查看更多
刘海飞了
5楼-- · 2018-12-31 10:58

In my case I use a web hosting but it´s the same in local host, I used:

ps -aef | grep 'node' 

for watch the node process then, the console shows the process with PID. for kill the process you have to use this command:

kill -9 PID

where PID is the process id from the command above.

查看更多
只若初见
6楼-- · 2018-12-31 10:58

Two servers can not listen on same port, so check out if other server listening on same port, also check out for browser sync if its running on same port

查看更多
十年一品温如言
7楼-- · 2018-12-31 10:59

This error comes when you have any process running on a port on which you want to run your application.

how to get which process running on that port=> command: sudo netstat -ap | grep :3000

output : you will get the process information which is using that port

tcp 0 0 IPaddress:3000 : LISTEN 26869/node

Now you can kill that process sudo kill -9 26869

查看更多
登录 后发表回答