Angular 8 并使用代理

Angular 8 and using a proxy

我以前从未使用过代理,所以这是一个非常基本的问题。我制作了在 localhost:2000 上提供的 Angular 应用程序并设置了所有路由,但后来意识到我必须像这样使用代理 (localhost:3000/api) :

{
  "/api": {
    "target": "http://localhost:3000",
    "secure": false
  }
}

我很困惑,因为我的应用程序中没有配置任何指向 /api 的路由。我希望对我的应用程序发出的任何请求都通过此 localhost:3000/api(不仅仅是那些在 URL 中具有 /api 的请求,因为它们是非现有),而无需更改太多我的路由模块文件。

您可以使用 "pathRewrite": {"^/api" : ""} 并更改您的配置,如下所示:

{
  "/api": {
    "target": "http://localhost:3000",
    "secure": false,
    "pathRewrite": {"^/api" : ""}
  }
}

请求应该是:

this.http.get<Parent>(api/<end-point>);