react項目中,package.json中proxy的配置以下html
"proxy": { "/api/rjwl": { "target": "http://47.94.142.215:8081", "changeOrigin": true } }
就會報這樣的錯誤react
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.git
緣由:github
原來React
新版本不支持那樣設置反向代理了npm
解決方案json
安裝 http-proxy-middleware
api
$ npm install http-proxy-middleware --save
$ # or
$ yarn add http-proxy-middleware
而後app
在建立一個setupProxy.js
文件,在src
目錄,src/setupProxy.js
ui
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
,spa
則須要加上這個"changeOrigin":true
不然則會報錯
參考地址:
https://www.blyoo.com/4007.html
項目
https://github.com/besswang/rj_cash_admin