首頁 >web前端 >js教程 >為什麼我在執行 NodeJS 伺服器並發出 XMLHttpRequest 時收到「錯誤:listen EADDRINUSE」?

為什麼我在執行 NodeJS 伺服器並發出 XMLHttpRequest 時收到「錯誤:listen EADDRINUSE」?

Barbara Streisand
Barbara Streisand原創
2024-11-14 20:06:02662瀏覽

Why Am I Getting

使用NodeJS 時解決「錯誤:監聽EADDRINUSE」

問題描述:
嘗試在連接埠80 上執行伺服器並且同時執行伺服器發出XMLHttpRequest 請求時,遇到「Error: Listen EADDRINUSE」。儘管瀏覽器能夠在伺服器運行時繼續瀏覽互聯網。

伺服器實作:

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

請求實作:

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

解決方案:

解決方案:
killall -9 node
要解決此問題,請考慮使用以下命令終止Nodejs 進程:

ps ax
請注意此命令將突然結束系統進程。若要驗證終止,請執行:

以上是為什麼我在執行 NodeJS 伺服器並發出 XMLHttpRequest 時收到「錯誤:listen EADDRINUSE」?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn