react配置代理,跨域

1 react porxy 只代理一个

修改package.json文件
"proxy":"http://172.19.5.35:9536",

2 react配置多个代理,跨域

1):安装http-proxy-middleware管理包,
cnpm http-proxy-middleware --save

npm install http-proxy-middleware --save

$ yarn add http-proxy-middleware
2):在项目目录src/下新建setupProxy.js文件,然后写入如下代码:
const proxy = require('http-proxy-middleware')

module.exports = function (app) {  
  app.use(proxy('/banner', {
    target: 'https://ad.maoyan.com/',
    secure: false,
    changeOrigin: true,
    pathRewrite: {
      "^/banner": "/"
     },
  }))
  app.use(proxy('/common', {
    target: 'https://api.maoyan.com',
    secure: false,
    changeOrigin: true,
    pathRewrite: {
      "^/common": "/"
     },
  }))
  app.use(proxy('/city', {
    target: 'http://m.maoyan.com/',
    secure: false,
    changeOrigin: true,
    pathRewrite: {
     "^/city": "/"
    },
  }))
}

你可能感兴趣的:(react配置代理,跨域)