如何明确使用localhost作为服务器,并同时允许将React应用程序代理到Go后端?

huangapple go评论136阅读模式
英文:

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"

huangapple
  • 本文由 发表于 2023年5月22日 14:15:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76303447.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定