Overcome CORS in Angular in your Local Environment

Jones Prabu Justin Raj
1 min readAug 25, 2022

--

A better solution is to use the PROXY in Angular to overcome CORS, instead of enabling the CORS in the server.

1. Add a proxy.config.js file in the root folder.

2. Add `”proxyConfig”: “proxy.config.js”` in the angular.json file under the serve: So when u run ng serve the poxy will be added to the angular node lite server.

3. In your api call you have to call the api as `http://localhost:4200/api/path` so from the browser it will be calling the apis in 4200, so the CORS err cannot come as now its calling the same domain.
In the node lite server, it will change the url to `http://localhost:8888/path` and call the api as a server-to-server call. You can see the ‘api’ in the URL will be cut off by the proxy server and change the target as provided in the config.

--

--

No responses yet