【问题标题】:How can I solve EADDRINUSE in nodejs?如何解决 nodejs 中的 EADDRINUSE?
【发布时间】:2022-01-26 10:38:43
【问题描述】:

我声明我使用的是 linux 操作系统,特别是我使用的是带有 linux-zen 内核的 Garuda linux。我在我的 arch 操作系统和 debian 操作系统(Ubuntu 21.10)中发现了这个问题,但在 windows 和 macOS 中没有。我以这种方式创建了一个 javascript 服务器:

const host = 'localhost';
const port = 8000;
const http=require('http')
const fs = require('fs')
const express = require("express");
const cors = require("cors");
const app = express();



const requestListener = function (req, res) {
  res.writeHead(200);
  res.end()
}

const server = http.createServer(requestListener);

const corsOptions = {
  origin: "http://localhost:4200",
  optionsSuccessStatus: 200, // For legacy browser support
  methods: "GET, POST"
};
app.use(cors(corsOptions));


app.listen(port, function() {
  console.log("Listening on " + port);
});
server.listen(port, host, () => {
  console.log(`Server is running on http://${host}:${port}`);
});

当我尝试使用node httpController.js 运行此服务器时,我的终端给了我这个错误:

Listening on 8000
node:events:368
      throw er; // Unhandled 'error' event
      ^

Error: listen EADDRINUSE: address already in use ::1:8000
    at Server.setupListenHandle [as _listen2] (node:net:1330:16)
    at listenInCluster (node:net:1378:12)
    at GetAddrInfoReqWrap.doListen [as callback] (node:net:1516:7)
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (node:dns:74:8)
Emitted 'error' event on Server instance at:
    at emitErrorNT (node:net:1357:8)
    at processTicksAndRejections (node:internal/process/task_queues:83:21) {
  code: 'EADDRINUSE',
  errno: -98,
  syscall: 'listen',
  address: '::1',
  port: 8000
}

Node.js v17.3.0

我认为在我的端口 8000 中运行了一个服务器,所以我在终端中输入了这个命令 lsof -i tcp:8000 但它没有给我任何响应可能是因为我的端口中没有运行任何服务器。为了更安全,我更改了服务器的端口,但响应相同(EADDRINUSE)。我能做什么?

附:这是netstat -tulpn的输出:

Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 127.0.0.1:46239         0.0.0.0:*               LISTEN      8612/app --node-int 
udp        0      0 0.0.0.0:59437           0.0.0.0:*                           9540/firefox        
udp        0      0 0.0.0.0:35238           0.0.0.0:*                           9540/firefox        
udp        0      0 0.0.0.0:52008           0.0.0.0:*                           9540/firefox        
udp        0      0 0.0.0.0:56124           0.0.0.0:*                           9540/firefox        
udp        0      0 0.0.0.0:43991           0.0.0.0:*                           -                   
udp        0      0 0.0.0.0:56391           0.0.0.0:*                           9540/firefox        
udp        0      0 0.0.0.0:56902           0.0.0.0:*                           9540/firefox        
udp        0      0 0.0.0.0:53119           0.0.0.0:*                           9540/firefox        
udp        0      0 0.0.0.0:41038           0.0.0.0:*                           9540/firefox        
udp        0      0 0.0.0.0:58315           0.0.0.0:*                           9540/firefox        
udp        0      0 0.0.0.0:5353            0.0.0.0:*                           -                   
udp        0      0 0.0.0.0:54602           0.0.0.0:*                           9540/firefox        
udp6       0      0 :::42912                :::*                                -                   
udp6       0      0 fe80::a715:e177:7d3:546 :::*                                -                   
udp6       0      0 :::5353                 :::*                                -          

这是strace -e trace=network node httpController.js的输出:

socket(AF_INET6, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, IPPROTO_IP) = 20
setsockopt(20, SOL_SOCKET, SO_REUSEADDR, [1], 4) = 0
setsockopt(20, SOL_IPV6, IPV6_V6ONLY, [0], 4) = 0
bind(20, {sa_family=AF_INET6, sin6_port=htons(8000), sin6_flowinfo=htonl(0), inet_pton(AF_INET6, "::", &sin6_addr), sin6_scope_id=0}, 28) = 0
listen(20, 511)                         = 0
Listening on 8000
socket(AF_INET6, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, IPPROTO_IP) = 21
setsockopt(21, SOL_SOCKET, SO_REUSEADDR, [1], 4) = 0
setsockopt(21, SOL_IPV6, IPV6_V6ONLY, [0], 4) = 0
bind(21, {sa_family=AF_INET6, sin6_port=htons(8000), sin6_flowinfo=htonl(0), inet_pton(AF_INET6, "::1", &sin6_addr), sin6_scope_id=0}, 28) = -1 EADDRINUSE (Indirizzo già in uso)
node:events:368
      throw er; // Unhandled 'error' event
      ^

Error: listen EADDRINUSE: address already in use ::1:8000
    at Server.setupListenHandle [as _listen2] (node:net:1330:16)
    at listenInCluster (node:net:1378:12)
    at GetAddrInfoReqWrap.doListen [as callback] (node:net:1516:7)
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (node:dns:74:8)
Emitted 'error' event on Server instance at:
    at emitErrorNT (node:net:1357:8)
    at processTicksAndRejections (node:internal/process/task_queues:83:21) {
  code: 'EADDRINUSE',
  errno: -98,
  syscall: 'listen',
  address: '::1',
  port: 8000
}

Node.js v17.3.0
+++ exited with 1 +++

【问题讨论】:

  • edit您的问题,并添加更多详细信息,您做了什么来得到这个错误。问题是否仅在您重新启动已连接的服务器时出现?
  • 我第一次尝试在我的 linux 机器上启动我的服务器,它给了我这个错误。之前从未启动过
  • edit你的问题添加要求的信息。我建议创建一个minimal reproducible example,然后我们可以尝试重现问题。
  • 我的问题有什么不清楚的地方?我在 javascript 中的端口 8000 上创建了一个本地服务器,只有并且只有我已经发布的代码(缺少的是 get 和 set)。我试图在所有操作系统中运行服务器,但只有在 linux 下它不起作用,它给了我错误 EADDRINUSE。我一直在查看该端口上是否有任何活动服务器,但端口 8000 上没有任何类型的活动服务器。
  • umm 在同一个端口上启动 2 个服务器,因此出现错误。不要传递无用的 requestListener ,而是将 app 传递给 http.createServer,然后删除 app.listen 行

标签: node.js linux shell server


【解决方案1】:

使用命令netstat -tulpn 查看 8000 端口中正在运行的服务。如果有任何服务正在运行,请尝试使用sudo kill -9 process_id 终止该服务以终止该进程。终止进程后,尝试在端口 8000 上运行您的服务器。希望这能解决您的问题。

【讨论】:

  • 我用 netstat 的输出编辑我的问题
  • 您应该先尝试不使用-9kill,以便让进程有机会进行清理。 kill -9 应该作为最后的手段,如果你不能通过其他方式停止进程
  • 我尝试杀死所有进程,然后再次运行node httpController.js,但它给了我相同的响应
猜你喜欢
  • 2019-05-29
  • 2015-04-28
  • 1970-01-01
  • 2023-03-02
  • 2018-02-03
  • 1970-01-01
  • 2016-07-12
  • 2019-10-28
  • 2016-08-28
相关资源
最近更新 更多