react项目中,package.json中proxy的配置如下
"proxy": { "/api/rjwl": { "target": "http://47.94.142.215:8081", "changeOrigin": true } }
就会报这样的错误
When specified, "proxy" in package.json must be a string.
Instead, the type of "proxy" was "object".Either remove "proxy" from package.json, or make it a string.
原因:
原来React
新版本不支持那样设置反向代理了
解决方案
安装 http-proxy-middleware
$ npm install http-proxy-middleware --save$ # or$ yarn add http-proxy-middleware
然后
在创建一个setupProxy.js
文件,在src
目录,src/setupProxy.js
const proxy = require('http-proxy-middleware')module.exports = function (app) { app.use(proxy('/rjwl', { target: 'http://47.94.142.215:8081', secure: false, changeOrigin: true, pathRewrite: { "^/rjwl": "/rjwl" } }))}
如果请求站点为https
,
则需要加上这个"changeOrigin":true
否则则会报错
参考地址:
https://www.blyoo.com/4007.html
项目
https://github.com/besswang/rj_cash_admin