鍍金池/ 問(wèn)答/HTML/ webpack devserver proxy 如何匹配代理多個(gè)路徑到不同hos

webpack devserver proxy 如何匹配代理多個(gè)路徑到不同host?

{
  devServer: {
    proxy: {
      '/api': {
        target: 'https://other-server.example.com',
        secure: false,
        changeOrigin: true,
        pathRewrite: {
                    "^/api": "/"
                }
      },
       '/test': {
        target: 'https://other-server.example2.com',
        secure: false,
        changeOrigin: true,
        pathRewrite: {
                    "^/test": "/"
                }
      }
    }
  }
}

這樣寫(xiě)貌似不能?求教如何做?

回答
編輯回答
萢萢糖

假設(shè)接口服務(wù)器在 https://other-server.example.com,本地服務(wù)器是 http://localhost:3000

原接口 https://other-server.example.com/api/get/something 代理到 http://localhost:3000/api/api/get/something

pathRewrite: { '^/api': '/' } // 重寫(xiě)了路徑為 `http://localhost:3000//api/get/something`

可能是重寫(xiě)規(guī)則錯(cuò)了導(dǎo)致 url 解析不正確

pathRewrite: { '^/api': '' } // 重寫(xiě)了路徑為 `http://localhost:3000/api/get/something`
2018年3月7日 18:34