react proxy 报错
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
只有在泥泞的道路上才能留下脚印

浙公网安备 33010602011771号