Troubleshooting "Error: listen EADDRINUSE" in Node.JS
When attempting to simultaneously run a server on port 80 and initiate an XMLHttpRequest, you may encounter the error "Error: listen EADDRINUSE." This issue arises when your local system attempts to allocate the same port to multiple applications, leading to a conflict.
In the provided server script:
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 issue lies in the server listening on port 80 while the XMLHttpRequest attempts to access the same port. To resolve the collision, you can consider using a different port for your server or for the XMLHttpRequest.
Additional troubleshooting steps:
The above is the detailed content of Why am I getting 'Error: listen EADDRINUSE' when running my Node.js server and making an XMLHttpRequest?. For more information, please follow other related articles on the PHP Chinese website!