小编Gue*_*875的帖子

从 NodeJS 运行 localhost 服务器时出现 ECONNREFUSED

我在本地计算机上启动并运行了一个 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 }
Run Code Online (Sandbox Code Playgroud)

我的请求如下所示,使用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 ...
Run Code Online (Sandbox Code Playgroud)

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

有人有主意吗?

javascript localhost node.js econnrefused

5
推荐指数
3
解决办法
2万
查看次数

标签 统计

econnrefused ×1

javascript ×1

localhost ×1

node.js ×1