Sitemap

Handling proxy in Angular

2 min readMar 15, 2020

--

In this article, let’s find the easy way of setting a proxy for angular front end projects.

Let’s assume we have a backend server running on https://localhost:5001.

I assume you already have an Angular project up and running, otherwise, we have to create a new one.

Inside the front end project, we want to make the request calls like this:

http.get<WeatherForecast[]>(‘/api/weatherforecast’).subscribe(result => {

this.forecasts = result;

}, error => console.error(error));

To achieve that, we need create a proxy json(proxy.conf.json)file like this:

{

“/api”: {

“target”: “https://localhost:5001",

“secure”: false

}

}

and then, we go to angular.json file to add the config file like this:

Get Jet丁Neat Coding’s stories in your inbox

Join Medium for free to get updates from this writer.

“serve”: {

“builder”: “@angular-devkit/build-angular:dev-server”,

“options”: {

“browserTarget”: “ding-client:build”,

“proxyConfig”: “proxy.conf.json”

},

“configurations”: {

“production”: {

“browserTarget”: “ding-client:build:production”

}

}

},

That’s all we need for code change.

Let’s check it out by ng serve to start your angular project, it will be able to make the api call to your backend.

Press enter or click to view image in full size

If you see any issues, please comment below.

--

--