英文:
How to use localhost explicitly as a server and in the same time allow proxying React app to backend in Go?
问题
我有一个用Go编写的后端。一切都正常,POSTMAN的调用也通过了,但突然间我无法让React应用程序调用这个服务器,因为出现了错误:
代理错误:无法将来自localhost:3000的请求代理到http://localhost:8080(ECONNREFUSED)。
我的Go服务器代码如下:
log.Fatal(http.ListenAndServe(fmt.Sprintf("localhost:8080"), api.router))
React应用程序的package.json如下:
"proxy": "http://localhost:8080"
我知道我可以像这样绑定Go中的所有接口:
log.Fatal(http.ListenAndServe(fmt.Sprintf(":8080"), api.router))
但是Mac OS会要求我允许此连接,由于我不是计算机的管理员,所以我无法这样做。
我可以使用codesign来解决这个问题,但是我目前没有任何身份,所以生成私钥并将其添加到密钥链中也需要一些时间。
有没有简单的方法可以绕过这个问题?
英文:
I have a backend written in Go. All works fine and calls from POSTMAN are passing, but suddenly I cannot have React app calling this server because of error:
Proxy error: Could not proxy request /login from localhost:3000 to http://localhost:8080 (ECONNREFUSED).
My go server code:
log.Fatal(http.ListenAndServe(fmt.Sprintf("localhost:8080"), api.router))
The package.json of React app looks like this:
"proxy": "http://localhost:8080"
I know that I could bind with all interfaces in Go like this:
log.Fatal(http.ListenAndServe(fmt.Sprintf(":8080"), api.router))
But then Mac OS asks me to Allow this connection and I cannot do it since I'm not the administrator on the computer
I could do it with codesign, but I don't have currently any identities so generating private key, adding it to key chain would also take some time
Any easy way to bypass this?
答案1
得分: 2
你尝试过在react代理行的末尾添加一个斜杠吗,像这样:
"proxy": "http://localhost:8080/"
上述内容是在一个类似你的问题的地方找到的这里
另外,你可以尝试将代理从localhost更改为127.0.0.1:
"proxy": "http://127.0.0.1:8080"
英文:
Have you tried adding a slash at the end of the react proxy line, like this:
"proxy": "http://localhost:8080/"
This above was found in a similar question to yours here
Also, you can try changing the proxy from localhost to 127.0.0.1:
"proxy": "http://127.0.0.1:8080"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论