1

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 的 package.json 不支持proxy是对象,只支持字符串

// 这样可以使用
"proxy": "http://wxlive.gaing.cn",

解决办法
安装 http-proxy-middleware

yarn add http-proxy-middleware

在创建一个setupProxy.js文件,在src目录,src/setupProxy.js

const proxy = require('http-proxy-middleware')

module.exports = function (app) {
    app.use(proxy('/api', {
      target: 'http://www.test.com',
      secure: false,
      changeOrigin: true,
      pathRewrite: {
        "^/api": "/api"
      }
    }))
}

完成,运行

yarn start 

作者:taoqun
地址:http://taoquns.com/paper/48


Taoqun
131 声望5 粉丝

[链接]