leslie1943/blog

网络安全: Node 正向代理

Opened this issue · 0 comments

代理的思路,利用服务端请求不会跨域的特性,让接口和当前站点同域

代理前
image

代理后
image

a. cli工具中的代理
  1. webpack(4.x): 在webpack中可以配置 proxy来快速获得接口代理的能力
  devServer: {
    port: 8000,
    proxy: {
      "/api": {
        target: "http://localhost:8080"
      }
    }
  },
  1. Vue-cli 2.x
proxyTable: {
  '/api': {
     target: 'http://localhost:8080',
  }
},
  1. Vue-cli 3.x => vue.config.js 如果没有就新建
devServer: {
    port: 8000,
    proxy: {
      "/api": {
        target: "http://localhost:8080"
      }
    }
  }

以上3种配置方式都有着共同的底层包 http-proxy-middleware

b. 使用自己的代理工具 cors-anywhere