如果我运行一个端口80的服务器,我尝试使用XMLHttpRequest,我得到这个错误

为什么NodeJS会有问题,如果我想做一个请求,而我在端口80上运行一个服务器?对于网络浏览器来说,这不是问题:我可以在服务器运行时上网。

服务器为:

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

当前回答

当我们意外地在同一个实例中有两个本地Express环境指向同一个端口时,得到此错误。

如果你已经找到了这些答案,我希望这将有助于解决你的问题。

其他回答

EADDRINUSE表示你的nodejs应用的端口已经被使用。

现在你已经杀死了在该端口上运行的进程/应用程序。 通过以下方式查找app的进程id:

-i tcp:3000

现在你会从这里得到进程id。 运行这个:

kill -9 进程 ID

我也有同样的问题,我只是关闭终端并打开一个新终端并运行

node server.js

一次。这对我来说是有效的,有时只需要等待几秒钟,直到它再次工作。

但这只适用于开发人员机器,而不是服务器控制台。

lsof -i:3000;
kill -9 $(lsof -t -i:3000);
// 3000 is a your port
// This "lsof -i:3000;" command will show PID 
kill PID 
ex: kill 129393

您的应用程序已经在端口8080上运行。 使用此代码杀死端口并再次运行您的代码

sudo lsof -t -i tcp:8080 | xargs kill -9

真正帮助我的是:

killall -9 node

但是这会杀死一个系统进程。

ps ax

你可以检查它是否有效。