Handle CORS in Angular 9

Sasitha Iresh
2 min readMay 9, 2020

--

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

Create a new project

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”.

Create “proxy.config.json” file

Then add the following lines to the files.

Add this code to the new file

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.

Multiple APIs’

Finally, before running the application, you have to add the proxy file to the serve command.

Add use proxy to serve command

--

--

Sasitha Iresh

Full-stack developer (Node, C#, Angular, React, Flutter), Traveler, Gamer