Home > Web Front-end > JS Tutorial > Why am I getting 'Error: listen EADDRINUSE' when running my Node.js server and making an XMLHttpRequest?

Why am I getting 'Error: listen EADDRINUSE' when running my Node.js server and making an XMLHttpRequest?

Barbara Streisand
Release: 2024-11-28 06:51:11
Original
572 people have browsed it

Why am I getting

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);
Copy after login

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();
Copy after login

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:

  • Ensure that no other applications are already listening on port 80.
  • Restart Node.JS and your web browser.
  • Check your firewall settings to make sure they are not blocking the port.
  • If necessary, forcefully terminate any Node.JS processes using killall -9 node. However, note that this should be used with caution as it can also kill other system processes.

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template