webpack devserver proxy 如何匹配代理多个路径到不同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": "/"
                }
      }
    }
  }
}

这样写貌似不能?求教如何做?

阅读 11k
1 个回答

假设接口服务器在 https://other-server.example.com,本地服务器是 http://localhost:3000

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

pathRewrite: { '^/api': '/' } // 重写了路径为 `http://localhost:3000//api/get/something`

可能是重写规则错了导致 url 解析不正确

pathRewrite: { '^/api': '' } // 重写了路径为 `http://localhost:3000/api/get/something`
推荐问题