Using Angular Proxy

Issue

Im trying to use angular proxy to proxy to my backend server.
My server is running on http://localhost:8000 My angular server is running on http://localhost:4200

If the API makes a call to http://localhost:4200/api/bacon. I want it proxied to http://localhost:8000/api/bacon. However I only want calls that start with /api to be proxied.

My proxy.conf.json file looks like this.

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

And one of my api calls will look like this.
http://localhost:4200/api/yemen

In this case I expect it to be proxied to
http://localhost:8000/api/yemen

However it does not seem to be working

Solution

The full URL gets forwarded to the target because you didn’t use pathRewrite, then you don’t have to write /api in the target property.

Also if you want to have more informations next time, you can use logLevel: 'debug' to see incoming requests and their redirections.

{
  "/api": {
    "target": "http://localhost:8000",
    "secure": false,
    "logLevel": "debug"
  }
}

Answered By – Guerric P

Answer Checked By – Mildred Charles (AngularFixing Admin)

Leave a Reply

Your email address will not be published.