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();
The error
EADDRINUSE
(Address already in use) reports that there is already another process on the local system occupying that address / port.There is a npm package called find-process which helps finding (and closing) the occupying process.
Here is a little demo code:
I prepared a small sample which can reproduce the
EADDRINUSE
error. If you launch the following program in two separate terminals, you will see that the first terminal will start a server (on port "3000") and the second terminal will close the already running server (because it blocks the execution of the second terminal,EADDRINUSE
):Minimal Working Example:
EADDRINUSE
means that the port(which we try to listen in node application) is already being used. In order to overcome, we need to identify which process is running with that port.For example if we are trying to listen our node application in 3000 port. We need to check whether that port is already is being used by any other process.
step1:
That the above command gives below result.
step2:
Now you got process ID(25315), Kill that process.
step3:
Note: This solution for linux users.
This works for me (I'm using mac). Run this command
lsof -PiTCP -sTCP:LISTEN
This's going to display a list of ports that your syetem is using. Find the
PID
that your node is runningand run
kill -9 [YOUR_PID]
** BEFORE DOWNVOTING - Please READ the answer. IT IS RELEVANT! If you are going to downvote this, leave a comment why you think it isn't relevant.
Just a head's up, Skype will sometimes listen on port 80 and therefore cause this error if you try to listen on port 80 from Node.js or any other app.
You can turn off that behaviour in Skype by accessing the options and clicking Advanced -> Connection -> Use port 80 (Untick this)
EADDRINUSE
means that the port number whichlisten()
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:On Debian i found out to run on port 80 you need to issue the command as root i.e
I hope it helps