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

相关文章
相关标签/搜索