如果我运行一个端口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();

当前回答

这发生在我身上是因为我的服务器在另一个终端窗口中运行。关闭连接解决了问题。

其他回答

解决方法:

1:需要使用以下命令终止进程。

pm2 kill <PROCESS_ID>

2:执行如下命令重新启动服务。

启动app.js——name“servername”

3:执行以下命令检查服务器状态。

pm2列表

Sudo kill $(Sudo lsof -t:80)

对于武力杀伤

Sudo kill -9 $(Sudo lsof -t:80)

使用上述CMD杀死特定端口,然后运行您的服务器

另一个可能产生此错误的是相同节点代码中的两个HTTP服务器。我正在更新一些Express 2到Express 3的代码,然后有了这个…

http.createServer(app).listen(app.get('port'), function(){            
  console.log('Express server listening on port ' + app.get('port')); 
});        

// tons of shit.

http.createServer(app).listen(app.get('port'), function(){            
  console.log('Express server listening on port ' + app.get('port')); 
});                                                                   

它触发了这个错误。

在Debian上,我发现要在端口80上运行,你需要以根用户的身份发出命令

sudo node app.js

我希望这对你们有帮助

这适用于我(我使用mac)。执行此命令

lsof -PiTCP -sTCP:LISTEN

这将显示系统正在使用的端口列表。找到您的节点正在运行的PID

COMMAND     PID          USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
node      17269 hientrq   16u  IPv6 0xc42959c6fa30c3b9      0t0  TCP *:51524 (LISTEN)
node      17269 hientrq   19u  IPv4 0xc42959c71ae86fc1      0t0  TCP localhost:1337 (LISTEN)

运行kill -9 [YOUR_PID]