简体   繁体   English

Http 从节点服务器到 Strapi 的请求抛出错误“econnrefused”

[英]Http request from node server to Strapi throws error "econnrefused"

I'm trying to access a strapi api in my local dev environement.我正在尝试在本地开发环境中访问 strapi api。 Wheras I have no issues to access the api under the same url with postman, trying to access it from my node server i run into this error: Wheras 我在同一个 url 和 postman 下访问 api 没有问题,试图从我的节点服务器访问它我遇到了这个错误:

connect ECONNREFUSED ::1:1337
    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1300:16) {
  errno: -4078,
  code: 'ECONNREFUSED',
  syscall: 'connect',
  address: '::1',
  port: 1337

This is the part of my server where I make the request:这是我发出请求的服务器的一部分:

const request = require('request');
        request({
      url: 'http://localhost:1337/api/homepage?populate=*',
      headers: {
         'Authorization': 'Bearer 123455347'  },
      rejectUnauthorized: false
    }, function(err, res) {
          if(err) {
            console.error(err);
          } else {
            console.log(res.body);
          }
    
    });
    }

Why refuses strapi the connection?为什么拒绝 strapi 连接? I'm helpless.. Ist there something I missed while configuring it?我很无奈..配置时我错过了什么吗?

I had the exact same issue and I have found a work around inspired by a discussion on GitHub ( https://github.com/axios/axios/issues/3821 ).我遇到了完全相同的问题,我发现了一个受 GitHub ( https://github.com/axios/axios/issues/3821 ) 讨论启发的解决方法。

The problem seems to be that Strapi does not support IPv6 properly with all Node-Versions ( https://github.com/strapi/strapi/issues/12285 ).问题似乎是 Strapi 不支持所有节点版本的 IPv6( https://github.com/strapi/strapi/issues/12285 )。

When I try to access my Strapi-API with the address http://localhost:1337 my computer resolves the IPv6 address::1: and this causes the beforehand mentioned error.当我尝试使用地址 http://localhost:1337 访问我的 Strapi-API 时,我的计算机解析了 IPv6 地址::1:,这导致了前面提到的错误。

connect ECONNREFUSED ::1:1337

Therefore instead of trying to connect to http://localhost:1337 try to use the IPv4 address of your computer.因此,与其尝试连接到 http://localhost:1337,不如尝试使用计算机的 IPv4 地址。 In my case this meant to connect to http://192.168.0.12:1137 .在我的例子中,这意味着连接到http://192.168.0.12:1137

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

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