美文网首页
react配置代理,跨域

react配置代理,跨域

作者: 温柔了十方春冬 | 来源:发表于2019-08-10 13:53 被阅读0次

    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配置代理,跨域

          本文链接:https://www.haomeiwen.com/subject/gpkrjctx.html