1

My development environment angular server runs on localhost:4200 and spring boot server runs on localhost:8080. Angular service invokes rest api calls using

this.http.get(window.location.protocol + '//' + window.location.hostname + ':8080/' +'<context-path>/api/<endpoint>');

Proxy configuration file:

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

I am starting my server using

ng serve --proxy-config proxy.conf.json

Intentionally I provided port number as 800, Still I can get results from server. This made sure for me that proxy configuration file is not being read.

When we can directly access the url from angular service, why do we need proxy configuration file?

1 Answer 1

1

The use of proxy file is basically telling angular to check the request's url and replace it with the target if matched.
In your case, try requesting this.http.get('api/something'), you'll see that there is a redirection made towards http://localhost:800.

To answer your last question :
Accessing the url directly from the angular service may seem convenient for local development. But once you go and set your back-end on a different server than your front-end app, it can save you loads of time trying to figure out why there's a CORS error.

Sign up to request clarification or add additional context in comments.

1 Comment

I can see url is redirecting to localhost:800 on the angular console throws an error on the console. After changing url to localhost:8080 in the proxy configuration file, I can see [HPM] /api/<end-point> -> http://localhost:8080/ on the angular console. Request does not hit server and on the chrome dev tools, I can see error: Http failure response for http://localhost:4200/api/<end-point>: 404 Not Found" name: "HttpErrorResponse" ok: false status: 404 statusText: "Not Found" url: "http://localhost:4200/api/<endpoint>"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.