'Angular 12 - Issue with proxy

I am currently struggling with the proxy configuration for my Angular 12 project. Basically, my local application runs on http://localhost:4200, and is calling a local back-end application which runs on http://localhost:8080 as follows:

http://localhost:4200 -> http://localhost:8080/dtm_be_war/api/v1/user

http://localhost:4200 -> http://localhost:8080/dtm_be_war/api/v1/user/login

http://localhost:4200 -> http://localhost:8080/dtm_be_war/api/v1/user/logout

Of course, this forces me to have my BE application running on separate server every time, therefore I was setting up mocked json-server which runs on http://localhost:3000 to call instead. The following is what I would like to obtain:

http://localhost:4200 -> http://localhost:8080/dtm_be_war/api/v1/user -> http://localhost:3000/user

http://localhost:4200 -> http://localhost:8080/dtm_be_war/api/v1/user/login -> http://localhost:3000/user/login

http://localhost:4200 -> http://localhost:8080/dtm_be_war/api/v1/user/logout -> http://localhost:3000/user/logout

I tried configuring a normal proxy.conf.json file by looking at several guides, but it doesn't seem to work. Please find my sample:

{
  "/dtm_be_war/api/*/user": {
    "target": "http://localhost:3000/user",
    "secure": false,
    "logLevel": "debug",
    "changeOrigin": true,
    "pathRewrite": {
      "^/dtm_be_war/api/*/": "/"
    }
  }
}

Command in package.json: "start:mock-admin": "concurrently \"ng serve --proxy-config proxy.conf.json\" \"json-server --watch src/app/core/mock/db-admin.json\ --routes src/app/core/mock/routes.json" "

Basically, I would like to make the json-server independent by the version of the api (i.e. v1), since it would meaningless for it.

I believe my configuration is wrong, although the proxy is captured fine, looking at the logs.

Could you please help me?

Thank you in advance.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source