Angular 17 ng serve proxy returns 302 instead of forwarding

33 views Asked by At

I'm trying to debug a Single Page Application and it's interactions with a Backend server. At the moment, I'm running the backend server on localhost:8082 and ng serve runs on localhost:4200.

Here is the proxy.conf.json file I'm currently using:

{
  "/api/**": {
    "target": "http://localhost:8082",
    "secure": false,
    "logLevel": "debug",
    "changeOrigin": true,
    "pathRewrite": {"^/api" : ""}
  },
  "/subject/**": {
    "target": "http://localhost:8080",
    "secure": false,
    "logLevel": "debug",
    "changeOrigin": true,
    "pathRewrite": {"^/subject" : ""}
  }
}

Here is the current serve block in angular.json:

"serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "configurations": {
            "production": {
              "buildTarget": "falsehoods-client:build:production"
            },
            "development": {
              "buildTarget": "falsehoods-client:build:development",
              "proxyConfig": "proxy.conf.json"
            }
          },
          "defaultConfiguration": "development"
        }

What I usually see is when I make a request to the backend server and use http://localhost:4200/api/** to do it, I usually see the 4200 server simply forward it to whatever backend server is specified in the target - in this case, on port 8082.

Now, here is what I'm seeing:

Example of 302 response instead of forwarding in action

Basically, I'm seeing a 302 response causing the browser to try to reach out to the backend server (and with more than the api portion of the path cut out for some reason.)

I know for a fact that the proxy config file is being used because when I tried omitting the changeOrigin fields, the location header in the 302 response would refer to localhost:4200 instead.

Any ideas on why this could be happening?

0

There are 0 answers