API呼叫适用于IPv4地址(127.0.0.1),但获取“错误:Connect Econnrefused :: 1:3001”使用Localhost时

发布于 2025-02-02 22:17:31 字数 952 浏览 6 评论 0原文

因此,我有一个非常简单的API调用,使用我的前端上的获取到http:// localhost:3001/test,这给了我一个错误:错误:连接econnrefused :: 1:3001

但是,当我直接调用该API时(直接输入API URI到我的浏览器)时,它可以正常工作。同样,当我将本地主机更改为http://127.0.0.1:3001/test在我的前端获取呼叫上,这也有效。

这似乎是一定是一个网络错误,因为:: 1和127.0.0.1解决到同一地址,但一个是IPv4,另一个是IPv6,对吗?有人对为什么会这样有何想法?

前端fetch(Backend_url = http:// localhost:3001):

export async function getStaticProps() {
  const res = await fetch(`${BACKEND_URL}/explore`, {
    method: 'GET',
    headers: {
      "Content-Type": 'application/json',
      Origin: BASE_URL,
    },
  });

  ...
}

端口服务器在端口3001上侦听(port = 3001):

const PORT = process.env.PORT;
app.listen(PORT, '0.0.0.0', () => {
    console.log(`Server is running on port ${PORT}`);
});

stack: stack:nextjs frontend,expressjs backend,expressjs backend,mongodb atlas db,nextauth,NextAuth,for Auth for Auth

So I have a very simple API call using fetch on my frontend to http://localhost:3001/test that gives me an error: Error: connect ECONNREFUSED ::1:3001

However, when I call that API directly (enter the api uri directly into my browser), it works just fine. Also when I change localhost to http://127.0.0.1:3001/test on my frontend fetch call, that works too.

This seems like it's gotta be a network error since ::1 and 127.0.0.1 resolve to the same address but one is IPv4 and the other is IPv6 right? Anyone have any thoughts on why this could be?

frontend fetch (BACKEND_URL = http://localhost:3001):

export async function getStaticProps() {
  const res = await fetch(`${BACKEND_URL}/explore`, {
    method: 'GET',
    headers: {
      "Content-Type": 'application/json',
      Origin: BASE_URL,
    },
  });

  ...
}

Backend Server listening on port 3001 (PORT = 3001):

const PORT = process.env.PORT;
app.listen(PORT, '0.0.0.0', () => {
    console.log(`Server is running on port ${PORT}`);
});

Stack: NextJS frontend, ExpressJS backend, MongoDB Atlas DB, NextAuth for auth

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

纵情客 2025-02-09 22:17:32

有几件事可能是问题:

  1. 您需要在括号之间包装IPv6地址,例如http:// [::: 1]:3001/Test
  2. Service仅在IPv4地址上侦听,而不是在IPv6地址上听。根据服务器的不同,您可能需要以不同的方式配置收听地址,

您的帖子不包含足够的信息来详细介绍。请编辑您的帖子以包括实际的代码,服务和配置,以便我们可以进一步帮助您。

A couple of things can be the issue:

  1. You need to enclose the IPv6 address between brackets, like http://[::1]:3001/test
  2. The service is only listening on the IPv4 address and not on the IPv6 address. Depending on the server you may need to configure the listening address differently

Your post doesn’t contain enough information to go into more detail. Please edit your post to include the actual code, service and configuration so we can help you further.

说好的呢 2025-02-09 22:17:32

当您在后端URL中使用“ localhost”时,默认情况下将解决到IPv6地址:: 1。但是,查看后端服务器代码,该后端正在以0.0.0.0为ipv4侦听。

您需要使该后端在IPv6上收听:

const PORT = process.env.PORT;
app.listen(PORT, '::', () => {
    console.log(`Server is running on port ${PORT}`);
});

When you use "localhost" in the backend URL, that is by default going to be resolved to the IPv6 address ::1. However, looking at the backend server code, that backend is listening on 0.0.0.0 which is IPv4.

You need to make that backend listen on IPv6:

const PORT = process.env.PORT;
app.listen(PORT, '::', () => {
    console.log(`Server is running on port ${PORT}`);
});
风吹雨成花 2025-02-09 22:17:32

这两个答案都指向该问题,您的服务器仅在收听IPv4 IP,但您的“ Localhost”转换为IPv6,

一种检查Mac/linux中映射的一种方法正在检查/etc/hosts的内容,您将是这样:

##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##
127.0.0.1   localhost
255.255.255.255 broadcasthost
::1             localhost

最后一个IP(:: 1)是Localhost的IPv6格式。
快速黑客是从主机文件中评论IPv6,然后尝试是否解决该问题(只需在该行的开头添加#):

127.0.0.1   localhost
255.255.255.255 broadcasthost
#::1             localhost

如果此问题解决此问题,并且您仍然需要使用IPv6,则必须查看配置您的服务器(但有时我们无法控制服务器的配置方式,因此可以解决问题)

Both answers pointed to the issue, you server is only listening to IpV4 ip but your "localhost" is translated into IpV6

One way to check your mappings in Mac/Linux is checking the content of /etc/hosts, you will something like this:

##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##
127.0.0.1   localhost
255.255.255.255 broadcasthost
::1             localhost

The last ip (::1) is the IPv6 format for localhost.
A quick hack is to comment the IPv6 from your hosts file and try if that fix the issue (simply add # at the beginning of that line):

127.0.0.1   localhost
255.255.255.255 broadcasthost
#::1             localhost

If this fix the issue and you still need to use IPv6 then you have to review the configuration of your server (but sometimes we don't have control on how our servers are configured so this would do the trick)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文