简体   繁体   English

如何在CRA v2中将代理值从package.json移动到setupProxy.js?

[英]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.

相关问题 如何使用CRA在package.json的“ proxy”中添加对象? - How to add object in “proxy” in package.json using CRA? 使用 http-proxy-middleware 配置 setupProxy.js - Configuring setupProxy.js using http-proxy-middleware 为不同的环境反应 setupProxy.js - React setupProxy.js for different environments 在http-proxy-middleware的setupProxy.js中反应导入环境,生成意外标识符 - react import environment in setupProxy.js of http-proxy-middleware generate unexpected identifier 我的 React 应用程序没有使用 http-proxy-middleware 注册 setupProxy.js - My React app is not registering setupProxy.js with http-proxy-middleware setupProxy.js 路径在使用 http 代理中间件的 reactjs 应用程序中无法识别 - setupProxy.js path are not recognizing in reactjs application using http proxy middleware 为什么我的后端代理(使用 setupProxy.js 文件设置)没有代理到正确的端口? - Why is my backend proxy (setup using a setupProxy.js file) not proxying to the right port? 如何在 next.js 中的 package.json 中使用代理? - How to use proxy in package.json in next.js? 当我制作 setupProxy.js 时,本地主机拒绝连接 - Localhost refuses to connect when I make setupProxy.js Typescript setupProxy.js 带有自定义 webPack 配置 - Typescript setupProxy.js whit custom webPack config
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM