繁体   English   中英

ECONNREFUSED 从 NodeJS 运行本地主机服务器

[英]ECONNREFUSED on running localhost server from NodeJS

我在本地机器上启动并运行了一个 NodeJS 服务器,它正在侦听端口 50000。从另一台也在我本地机器上运行的服务器,我需要向该服务器发出一个简单的 GET 请求,但我得到的只是一个ECONNREFUSED 错误:

{ Error: connect ECONNREFUSED 127.0.0.1:50000
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1106:14)
  errno: 'ECONNREFUSED',
  code: 'ECONNREFUSED',
  syscall: 'connect',
  address: '127.0.0.1',
  port: 50000 }

我的请求如下所示,使用request

var options = {
    url: "http://localhost:50000",
    method: "GET"
}
request(options, function(error, response, body) {
    if (error) {
        console.log("[" + getDateTime() + "] Error connecting to localhost:");
        console.log(error);
        return;
   }
   // continue ...

我知道服务器已启动并正在运行并且端点已定义,因为我可以在 postman 或在我的浏览器中向完全相同的 url 发出请求并获得响应,但不知何故在我的 NodeJS 代码中没有。

有人有想法吗?

正如所评论的那样,将“localhost”更改为“http://127.0.0.1:50000”对我有用。 我将在此处留下一个链接,以获取对此问题的可能解释

https://github.com/node-fetch/node-fetch/issues/1624#issuecomment-1235826631

可能的问题是,其他进程已在您尝试使用的同一端口上运行,请更改端口或杀死该端口上的现有进程。 要终止端口上的进程,您可以尝试:

对于Mac:

sudo kill $(lsof -t -i:8000) 
# or 
sudo fuser -k -n tcp 8000 
# or 
fuser -k 8000/tcp

对于Windows,请检查

希望这可以帮助 :)

您可能在同一端口上同时运行两个服务器,而在同一端口上杀死了另一台服务器。

如果您使用的是Linux,则可以使用sudo fuser -k -n tcp 5000杀死端口

或者如果您使用的是Windows: taskkill /PID 5000 /F

在代码的开头使用它:

const dns = require('dns');

// Set default result order for DNS resolution
dns.setDefaultResultOrder('ipv4first');

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM