API呼叫适用于IPv4地址(127.0.0.1),但获取“错误:Connect Econnrefused :: 1:3001”使用Localhost时
因此,我有一个非常简单的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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有几件事可能是问题:
您的帖子不包含足够的信息来详细介绍。请编辑您的帖子以包括实际的代码,服务和配置,以便我们可以进一步帮助您。
A couple of things can be the issue:
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.
当您在后端URL中使用“ localhost”时,默认情况下将解决到IPv6地址
:: 1
。但是,查看后端服务器代码,该后端正在以0.0.0.0
为ipv4侦听。您需要使该后端在IPv6上收听:
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 on0.0.0.0
which is IPv4.You need to make that backend listen on IPv6:
这两个答案都指向该问题,您的服务器仅在收听IPv4 IP,但您的“ Localhost”转换为IPv6,
一种检查Mac/linux中映射的一种方法正在检查/etc/hosts的内容,您将是这样:
最后一个IP(:: 1)是Localhost的IPv6格式。
快速黑客是从主机文件中评论IPv6,然后尝试是否解决该问题(只需在该行的开头添加#):
如果此问题解决此问题,并且您仍然需要使用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:
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):
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)