react proxy 報錯

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-middlewareapi

$ npm install http-proxy-middleware --save
$ # or
$ yarn add http-proxy-middleware

而後app

在建立一個setupProxy.js文件,在src目錄,src/setupProxy.jsui

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"
    }
  }))
}

若是請求站點爲httpsspa

則須要加上這個"changeOrigin":true

不然則會報錯

 

參考地址:

https://www.blyoo.com/4007.html

項目

https://github.com/besswang/rj_cash_admin

相關文章
相關標籤/搜索