API 在 netlify 上部署后来自代理不工作

API from proxy not working after deploying on netlify

我正在使用部署在 heroku 中的 api 到我的 React 应用程序。为了修复 cors 错误,我在 package.json 中添加代理作为

"proxy":"https://mobtech.herokuapp.com"

在调用 api 时,我是这样做的

 const res = await axios.get('/api/user')

因此,这样做在开发时效果很好,但在部署到 netlify 后就不行了。我从 netlify 文档 click 阅读了官方文档,但它并没有减轻我的想法。有人可以举个例子帮助我吗?

我可能迟到了,但这里是如何完成的。 Netlify 应该在你的根目录中有一个名为 __redirect 的特殊文件,你可以在那里定义你的代理,比如

/api/* https://yourapi.domain.com/:splat 200
/* /index.html 200           

然后你可以这样称呼你的api

const res = fetch('/api/user')   //equivalent to https://yourapi.domain.com/user

希望对您有所帮助