角度代理通配符无法正常工作。只适用于一条路线

发布于 2025-01-11 13:39:47 字数 471 浏览 0 评论 0原文

我创建了一个角度代理来在开发过程中绕过 CORS。问题是,代理似乎只适用于一条路线,而不适用于其他任何路线。除了代理配置之外,还有什么我应该注意的吗?我的后端位于云上,与我的开发环境分开。

这是我的代理的配置:

{
  "/api/*": {
    "target": "http://x.x.x.x:3000",
    "secure": false
  }
}

有效的路由是 /api/content 而其他所有内容(例如 /api/auth)只会给我以下错误:

XHR POST http://localhost:4200/api/auth

Status
  404
  Not Found
  VersionHTTP/1.1
  Transferred424 B (148 B size)
  Referrer Policystrict-origin-when-cross-origin

I have created an angular proxy to bypass CORS during development. The problem is, that the proxy seems to only apply to one route and it's not applied to anything else. Is there something aside the proxy config I should be aware of? My backend is located on cloud, separate from my development environment.

Here is the configuration for my proxy:

{
  "/api/*": {
    "target": "http://x.x.x.x:3000",
    "secure": false
  }
}

The route that works is /api/content while everything else (such as /api/auth) just gives me the following error:

XHR POST http://localhost:4200/api/auth

Status
  404
  Not Found
  VersionHTTP/1.1
  Transferred424 B (148 B size)
  Referrer Policystrict-origin-when-cross-origin

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦行七里 2025-01-18 13:39:47

这对我来说是一个愚蠢的错误,由错误说路由“http://localhost:4200/api/auth”是 404 引起的。这让我认为代理不起作用,因为在这种情况下我期望错误为“http://xxxx/auth”为 404。实际上,代理正在工作,但服务器上的 api 尚未创建这样的端点。在我为 /auth 创建 POST api 挂钩后,该错误已修复。

This was kind of a silly mistake on my part caused by the error saying that the route "http://localhost:4200/api/auth" was 404. This made me think that the proxy was not working because in that case i expected the error to be "http://x.x.x.x/auth" being 404. In reality the proxy was working, but the api on the server did not yet have such an end point created to it. The error was fixed after I created the POST api hook for /auth.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文