Home  >  Article  >  Web Front-end  >  Why Am I Getting 'Error: listen EADDRINUSE' When Running a NodeJS Server and Making an XMLHttpRequest?

Why Am I Getting 'Error: listen EADDRINUSE' When Running a NodeJS Server and Making an XMLHttpRequest?

Barbara Streisand
Barbara StreisandOriginal
2024-11-14 20:06:02591browse

Why Am I Getting

Resolving "Error: listen EADDRINUSE" while Utilizing NodeJS

Problem Description:
When attempting to run a server on port 80 and simultaneously make an XMLHttpRequest request, the "Error: listen EADDRINUSE" is encountered. This is despite browsers being able to continue browsing the internet while the server is running.

Server Implementation:

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

Request Implementation:

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();

Solution:
To resolve this issue, consider terminating the nodejs process using:

killall -9 node

Note that this command will abruptly end a system process. To verify the termination, execute:

ps ax

The above is the detailed content of Why Am I Getting 'Error: listen EADDRINUSE' When Running a NodeJS Server and Making an XMLHttpRequest?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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