简体   繁体   English

角度代理路径重写未按预期运行

[英]Angular proxy path rewrite not behaving as expected

I'm trying to build an angular app that reaches a backend server.我正在尝试构建一个可以到达后端服务器的角度应用程序。 I've set up an application proxy file so that my angular app could make calls to this backend.我已经设置了一个应用程序代理文件,以便我的 Angular 应用程序可以调用这个后端。

  {
    "/Review/*": {
      "target": "http://localhost:8082",
      "secure": false,
      "logLevel": "debug",
      "changeOrigin": true,
      "pathRewrite": {"^/Review" : ""}
    },
    "/Submit/*": {
      "target": "http://localhost:8081",
      "secure": false,
      "logLevel": "debug",
      "changeOrigin": true,
      "pathRewrite": {"^/Submit" : ""}
    }
}

Here is the Path I'm sending out:这是我要发送的路径:

http://localhost:4200/Submit/Auth/login

Here is the Expected and Actual Rewrite I'm seeing:这是我看到的预期和实际重写:

localhost:8081/Auth/login   (expected)
localhost:8081/login        (Actual)

Is there a reason why this is happening and a way to ensure that the pathRewrite handles long, complex endpoints in a predictable manner?是否有发生这种情况的原因以及确保pathRewrite以可预测的方式处理长而复杂的端点的方法? Like, just have it remove the submit ?就像,只是让它删除submit

Angular Version:
Angular CLI: 13.1.4
Node: 14.17.3
Package Manager: npm 6.14.13
OS: win32 x64

Note: I did come across This Stack-overflow question But I can't deduce what I'm doing wrong.注意:我确实遇到了This Stack-overflow question但我无法推断出我做错了什么。

things I've tried我尝试过的事情

  1. Removing the "pathRewrite" field and updating the backend to use the context path (Result: Same thing)删除“pathRewrite”字段并更新后端以使用上下文路径(结果:相同)
  2. Replacing the port with 8080 (Same thing, just with 8080 instead of 8081)用 8080 替换端口(同样的,只是用 8080 而不是 8081)
  3. Replacing Submit with submit (lowercase) (Result: Same thing)submit (小写)替换Submit (结果:相同)
  4. Using a JavaScript file where I implement my own pathRewrite:使用我实现自己的 pathRewrite 的 JavaScript 文件:
"pathRewrite":  function (path, req) {
        let ret = path.replace("/submit/", "/");
        console.log("Rewriting path {} to {}",path, ret);
        return ret;
      }

(Result: Same thing though I'm seeing this in the console Rewriting path {} to {} /submit/Auth/login /Auth/login ) (结果:虽然我在控制台中看到了同样的情况Rewriting path {} to {} /submit/Auth/login /Auth/login

This makes me wonder if maybe the problem isn't the path rewrite but some aspect of the software that uses it that I'm unaware of.这让我想知道问题是否可能不是路径重写,而是我不知道的使用它的软件的某些方面。

When My browser makes a request to 4200, 4200 responds with a 302 with location of http://localhost:8081/login当我的浏览器向 4200 发出请求时,4200 以http://localhost:8081/login的位置响应 302

I've done a little bit of digging and it turns out that the issue may have been on the Backend side.我做了一些挖掘,结果发现问题可能出在后端。 It kept returning 401.它一直返回401。

I fixed the Backend such that it could return 200 at the specified endpoint and now the Angular Proxy is behaving as expected.我修复了后端,使其可以在指定的端点返回 200,现在 Angular 代理的行为符合预期。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM