webpack 跨域代理设置

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u012733501/article/details/84936304

在config文件夹下的 index.js文件中 增加属性 proxyTable

dev: {
    // Paths
    assetsSubDirectory: 'static',
    assetsPublicPath: '/',
    proxyTable: {
      '/api': {
        target: "http://localhost:8760",
        changeOrigin: true,
        pathRewrite: {
          '^/api': ''
        },
      }
    },
}

假设controller为 ‘/User/’ ,其中的方法为 ‘getUserList’ 那么接口地址应该为
localhost:8760/User/getUserList 其中的/User/getUserList就是接口

其中 ‘/api’ 就是等于 target地址,
changeOrigin:true 就是开启跨域。
pathRewrite:重写地址。如果接口中有api呢,如跨服务:

zuul:
  prefix: /api
  routes:
    admin:
      path: /admin/**
      serviceId: admin

    ar:
      path: /ar/**
      serviceId: account-receivable
    pre:
      path: /pre/**
      serviceId: prepay

这个时候的接口地址就是/api/服务(admin等)/controller/方法名。
对比上面的例子,接口就是/api/admin/User/getUserList.
那么这个时候如果直接代理。就会造成少一个./api,因为/api
代替了target。那么接口中的/api没了,所以要重写地址,匹配出来一个/api

说到最后就是/api 代表target/api ,而最后一个/api 为“” 所以 /api代表target

猜你喜欢

转载自blog.csdn.net/u012733501/article/details/84936304