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`
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题