简体   繁体   English

create-react-app gh-pages 使用代理部署

[英]create-react-app gh-pages deploy with proxy

I have an API call like this:我有一个像这样的 API 调用:

 const response = await fetch('/api/some-url')

To make it work I added proxy field to package.json为了使它工作,我将代理字段添加到 package.json

...
"proxy": "https://some-domain.com"
...

I tried npm start and it worked well我试过npm start ,效果很好

After that I added deploy to gh-pages: added property "homepage" and installed "gh-pages" package (all from this https://create-react-app.dev/docs/deployment/ )之后,我将 deploy 添加到 gh-pages: added property "homepage" 并安装了 "gh-pages" package (全部来自此https://create-react-app.dev/docs/deployment/

Next, I ran npm run deploy and went to github where my app was being hosted (my-user-name.github.io/project-name).接下来,我运行npm run deploy并转到托管我的应用程序的 github(my-user-name.github.io/project-name)。 And saw the result of api call - 404 error.并看到 api 调用的结果 - 404 错误。 Request url of api call was 'my-user-name.github.io/project-name/api/some-url'.请求 api 调用的 url 是 'my-user-name.github.io/project-name/api/some-url'。

Here is a github repo in which I have a problem: https://github.com/kpodmasko/binance-test-solution这是一个 github 存储库,其中我有一个问题: https://github.com/kpodmasko/binance-test-solution

Questions: How to change request url from my-user-name.github.io/project-name/api/some-url to https://some-domain.com/api/some-url and save proxy logic?问题:如何将请求 url 从 my-user-name.github.io/project-name/api/some-url 更改为Z5E056C500A1C4B6A7110B50D807BADEsome-url并保存 proxy-domain.com/apisome-url://

I added env variable ( https://create-react-app.dev/docs/adding-custom-environment-variables/ ) to change my-user-name.github.io/project-name/api/some-url to https://some-domain.com/api/some-url .我添加了环境变量( https://create-react-app.dev/docs/adding-custom-environment-variables/ )以将 my-user-name.github.io/project-name/api/some-url 更改为https://some-domain.com/api/some-url

After that I have a problem with CORS.之后我遇到了 CORS 的问题。 The solution is to remove proxy property from package.json.解决方案是从 package.json 中删除代理属性。 And use fetch like this fetch('https://thingproxy.freeboard.io/fetch/https://some-domain.com/api/some-url') .并像这样使用fetch('https://thingproxy.freeboard.io/fetch/https://some-domain.com/api/some-url')

It works on localhost, but gh-pages - not.它适用于本地主机,但 gh-pages - 不是。 So I think that to make it work on gh-pages you should found working proxy.所以我认为要让它在 gh-pages 上工作,你应该找到工作代理。 Also, someone gave me advice that I can buy Digital Ocean hosting server and make it work.另外,有人给我建议,我可以购买 Digital Ocean 托管服务器并使其工作。

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

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