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

查看:23
本文介绍了如何修复错误:在使用 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

为什么当我在端口 80 上运行服务器时,nodejs 会出现问题,如果我想做一个请求?对于网络浏览器来说,这不是问题:我可以在互联网上冲浪,而服务器正在运行.

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.
Body length: " + this.responseText.length);
        sys.puts("Body:
" + this.responseText);
    }
};

xhr.open("GET", "http://mywebsite.com");
xhr.send();

推荐答案

前面提到的 killall -9 node,由 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.

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

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.

我今天正在阅读一个相当相关的主题,并在 为什么我不应该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天全站免登陆