如何使用Angular-CLI重写反向代理中的路径?

Nan*_*ano 7 proxy angular-cli angular

我已经使用angular2 CLI设置了反向代理,如下所示:

{
  "/api/customer/*": {
    "target": "http://localhost:9010",
    "secure": false
  }
}
Run Code Online (Sandbox Code Playgroud)

我的问题是远程API在路径/客户上公开服务,但反向代理发送的请求在/ api/customer上.

有没有办法从反向代理发送的请求中删除/ api?(不要回答"只需从你的http请求中删除/ api",因为我在/ customer上有一个有角度的路由).

谢谢,

纳米

ale*_*orf 12

您可以使用如下pathRewrite选项轻松完成此操作:

proxy: {
    '/api/customer/*': {
        target: 'http://localhost:9010',
        pathRewrite: {'^/api' : ''}
    }
}
Run Code Online (Sandbox Code Playgroud)

您还可以查看Webpack文档以获取更多信息.