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

问题描述 投票:4回答:1

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

{
  "/api/customer/*": {
    "target": "http://localhost:9010",
    "secure": false
  }
}

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

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

谢谢,

纳米

angular proxy angular-cli
1个回答
10
投票

使用pathRewrite选项可以很容易地做到这一点,如下所示:

proxy: {
    '/api/customer/*': {
        target: 'http://localhost:9010',
        pathRewrite: {'^/api' : ''}
    }
}

您还可以查看Webpack documentation以获取更多信息。

© www.soinside.com 2019 - 2024. All rights reserved.