首页 > 其他 > 详细

react 添加代理 proxy

时间:2019-12-13 17:50:56      阅读:147      评论:0      收藏:0      [点我收藏+]

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

react 添加代理 proxy

原文:https://www.cnblogs.com/taoquns/p/12036173.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!