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

当前回答

在控制器环境下,你可以使用:

在运行脚本之前,Pkill节点应该完成这项工作。

请记住,这个命令会杀死所有的节点进程,如果你有一个容器只运行一个实例,这可能是正确的,如果你有这样的环境,你可以保证。

在任何其他场景中,我建议使用命令终止通过编程方式查找的某个进程id或名称。比如,如果你的进程被调用node-server-1你可以执行pkill node-server-1。

这个资源可能有助于理解: https://www.thegeekstuff.com/2009/12/4-ways-to-kill-a-process-kill-killall-pkill-xkill/

其他回答

另一个可能产生此错误的是相同节点代码中的两个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')); 
});                                                                   

它触发了这个错误。

这个选项对我来说是有效的:

Run:

ps -ax | grep node

你会得到这样的东西:

 8078 pts/7    Tl     0:01 node server.js
 8489 pts/10   S+     0:00 grep --color=auto node    
 kill -9 8078

在我的情况下,Apache HTTP服务器运行在端口80上,我通过发出命令作为根来解决它

Sudo杀死所有HTTPD

更新

如果Jenkin已经安装并运行在你的Mac上;

可以使用sudo lsof -i tcp:8080进行检查 如果是,并且您只想停止Jenkins一次,请执行:sudo launchctl unload /Library/LaunchDaemons/org.jenkins-ci.plist

错误:listen EADDRINUSE表示要分配/绑定到应用服务器的端口已经在使用中。您可以为您的应用程序分配另一个端口。

或者如果你想给应用程序分配相同的端口。然后杀死在你想要的端口上运行的应用程序。

对于一个节点应用程序,你可以尝试的是,找到节点应用程序的进程id:

ps -aux | grep node

在获得进程id之后,执行

kill process_id

如果你想解决这个问题

$ node server events.js:141 throw er; // Unhandled 'error' event ^ Error: listen EADDRINUSE :::3000 at Object.exports._errnoException (util.js:907:11) at exports._exceptionWithHostPort (util.js:930:20) at Server._listen2 (net.js:1250:14) at listen (net.js:1286:10) at Server.listen (net.js:1382:5) at EventEmitter.listen (C:\sendbox\mean\node_modules\express\lib\application .js:617:24) at Object. (C:\sendbox\mean\server.js:28:5) at Module._compile (module.js:409:26) at Object.Module._extensions..js (module.js:416:10) at Module.load (module.js:343:32)

将端口号更改为8000