[英]How to move a proxy value from package.json to setupProxy.js in CRA v2?
Since recently released CRA (Create React App) v2 it's now possible to move proxy settings from package.json
to setupProxy.js
. 自从最近发布CRA(创建React App)v2以来,现在可以将代理设置从
package.json
移到setupProxy.js
。 My question is a simple one: how should my setupProxy.js
look like if I simply want to always use http://127.0.0.1:5000
? 我的问题很简单:如果我只想一直使用
http://127.0.0.1:5000
我的setupProxy.js
应该setupProxy.js
什么样? I need this because if everything works correctly (no luck so far) I can replace it with variable from .env
files for dev/prod environments. 我需
.env
因为如果一切正常(到目前为止没有运气),我可以将其替换为开发/生产环境的.env
文件中的变量。 But so far I only get either a or b: 但是到目前为止,我只得到a或b:
a) CORS errors a)CORS错误
b) my app's index.html
is returned as a result of http request b)由于http请求而返回了我的应用程序的
index.html
Here's an example of one of the versions of setupProxy.js I tried: 这是我尝试过的setupProxy.js版本之一的示例:
const proxy = require('http-proxy-middleware');
module.exports = function(app) {
app.use('/', proxy({target: 'http://127.0.0.1:5000', changeOrigin: true}));
app.use('/*', proxy({target: 'http://127.0.0.1:5000', changeOrigin: true}));
app.use(proxy({'/', target: 'http://127.0.0.1:5000', changeOrigin: true}));
app.use(proxy({'/*', target: 'http://127.0.0.1:5000', changeOrigin: true}));
};
I tried various versions of app.use
line, I just posted several examples here. 我尝试了各种版本的
app.use
行,我在这里发布了几个示例。
I usually tried with one app.use
line at a time, but also with multiple lines. 我通常一次尝试一个
app.use
行,但也尝试了多行。
I restarted react app everytime I changed the file. 每次更改文件时,我都会重新启动react app。
I also tried using process.env.REACT_APP_BACKEND_BASEURL
instead of 'http://127.0.0.1:5000'
which is my final goal. 我还尝试使用
process.env.REACT_APP_BACKEND_BASEURL
而不是'http://127.0.0.1:5000'
,这是我的最终目标。
所以尝试
app.use(proxy('/**', { target:'http://127.0.0.1:5000', changeOrigin: true }))
如果我没记错,使用setupProxy.js
仅对本地开发setupProxy.js
,但我也需要它在实时/部署服务器上工作,因此我最终根本不使用setupProxy.js
我将其他后端URL保存为REACT_APP_BACKEND_BASEURL
在不同的变量.env
文件(loclahost URL .env.development
文件,并在远程URL .env.production
文件),并取代了前端JavaScript代码来使用所有出现process.env.REACT_APP_BACKEND_BASEURL
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.