如何修复错误:在使用nodejs时听EADDRINUSE? [英] How to fix Error: listen EADDRINUSE while using nodejs?

查看:117
本文介绍了如何修复错误:在使用nodejs时听EADDRINUSE?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我使用端口80运行服务器,并尝试使用 xmlHTTPrequest ,则会出现此错误:错误:听EADDRINUSE

If I run a server with the port 80, and I try to use xmlHTTPrequest i get this error: Error: listen EADDRINUSE

为什么我在运行服务器的时候想要做一个请求的nodejs有问题在80号港口?对于网络浏览器来说这不是问题:我可以在互联网上冲浪,而服务器正在运行。

Why is it problem for nodejs, if I want to do a request, while I run a server on the port 80? For the webbrowsers it is not a problem: I can surf on the internet, while the server is running.

服务器是:

  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节点,Patrick的工作按预期工作并解决了问题,但您可能想要阅读这个非常答案的编辑部分,了解为什么 kill -9 可能不是最好的方法。

The aforementioned killall -9 node, suggested by Patrick works as expected and solves the problem but you may want to read the edit part of this very answer about why kill -9 may not be the best way to do it.

除此之外,您可能希望定位单个进程,而不是盲目杀死所有活动进程。

On top of that you might want to target a single process rather than blindly killing all active processes.

在这种情况下,首先获取在该端口上运行的进程的进程ID(PID)(比如8888):

In that case, first get the process ID (PID) of the process running on that port (say 8888):

lsof -i tcp:8888

这将返回如下内容:

COMMAND   PID    USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
node     57385   You   11u  IPv6 0xac745b2749fd2be3      0t0  TCP *:ddi-tcp-1 (LISTEN)

然后就这样做(ps - 实际上不要。请继续阅读下面的内容):

Then just do (ps - actually do not. Please keep reading below):

kill -9 57385

你可以阅读更多关于这个这里

You can read a bit more about this here.

编辑:我今天正在阅读一个相当相关的主题stumb在为什么我不能 kill -9 一个过程

I was reading on a fairly related topic today and stumbled upon this interesting thread on why should i not kill -9 a process.


一般情况下,你应该在 kill -9 之前使用 kill -15 给予目标过程有机会自行清理。 (进程无法捕获或忽略SIGKILL,但是它们可以并且经常捕获SIGTERM。)如果您没有给进程一个机会完成它正在进行的操作和清理,它可能会留下损坏的文件(或其他状态)一旦重新启动它将无法理解。

Generally, you should use kill -15 before kill -9 to give the target process a chance to clean up after itself. (Processes can't catch or ignore SIGKILL, but they can and often do catch SIGTERM.) If you don't give the process a chance to finish what it's doing and clean up, it may leave corrupted files (or other state) around that it won't be able to understand once restarted.

所以,如上所述,你最好用以下方法杀死上述过程:

So, as stated you should better kill the above process with:

kill -15 57385

编辑2 在评论中注明在这里很多次这个错误是不能优雅地退出进程的结果。这意味着,很多人使用 CTRL + Z 退出节点命令(或任何其他命令)。停止正在运行的进程的正确方法是发出执行干净退出的 CTRL + C 命令。

EDIT 2: As noted in a comment around here many times this error is a consequence of not exiting a process gracefully. That means, a lot of people exit a node command (or any other) using CTRL+Z. The correct way of stopping a running process is issuing the CTRL+C command which performs a clean exit.

以正确的方式退出进程关闭时释放该端口。这将允许您重新启动该过程,而无需在自己再次重新运行之前自行终止它。

Exiting a process the right way will free up that port while shutting down. This will allow you to restart the process without going through the trouble of killing it yourself before being able to re-run it again.

这篇关于如何修复错误:在使用nodejs时听EADDRINUSE?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆