Handle CORS in Angular 9
In this article, I will go through an easy way to handle the CORS issue when the angular app runs on the localhost.
First, we have to create a new angular project using Angular-CLI.
ng new test
This will create a new Angular project.
cd test
code .
I’m using the Visual Studio Code for development. That’s why I used “code .”. It will open a new Visual Studio Code window with the current folder(*/test/).
Then create a new file in the project folder and name it as “proxy.config.json”.
Then add the following lines to the files.
In the angular app if you are calling for “/api/**” URL use “http://localhost:4200/api” as your URL.
If you have multiple APIs’ to call then you can use something like this.
Finally, before running the application, you have to add the proxy file to the serve command.
Sample Github code link https://github.com/sir21/angular-proxy-medium