Proxy Routing in Angular 4 Applications

This blog intends to deal and simplify routing in Angular 4 applications in both development and production environment. While working on routing in Angular 4 applications, we often face some of the following challenges:

  • Finding a solution to the problem of cross-origin issues in Angular development environment
  • Separating routing configurations and APIs servers URLs from the code, making the application more robust and maintainable
  • Finding a generic solution where frequent changing of API URLs (due to deployment on different servers, let’s say for the purpose of load balancing) do not force you to modify and change the code
  • Servers where the APIs deployed do not appear in your code at all


Before we proceed with details on routing in Angluar 4 applications, I assume you meet the following prerequisites:

  • Familiarity or basic hands-on experience with Angular 2+ applications
  • Basic knowledge of how to make http requests (GET/POST/PUT/DELETE) in Angular


In development environment

The common approach:

– Create a service in your angular app and write the method from which the server URL can be fetched.


export class MyService {
return "http://localhost:4000/"

view raw


hosted with ❤ by GitHub

– Inject this service inside your component and use it wherever required:

import { Component, OnInit } from '@angular/core';
import { MyService } from '/myService.service';
selector: 'app-MyComponent-interface',
templateUrl: './MyComponent.component.html',
styleUrls: ['./MyComponent.component.css']
export class MyComponent implements OnInit {
constructor(private myService:MyService) { }
ngOnInit() {
//use response
let headers = new Headers({ 'Content-Type': 'application/json'});
let options = new RequestOptions({ headers: headers });
return this.http.get(this.myService.getBaseUrl()+ 'recentData', options).map(res=>res.json());

view raw


hosted with ❤ by GitHub

Disadvantages of the common approach:

  • To add new server URLs we must add methods in MyService or change the code inside the getBaseUrl() method
  • With the APIs being deployed on different servers, maintenance of servers is a tedious process.
  • With every URL change, the code in getBaseUrl() must be modified and the entire application where the API URL is used must be tested.

Solution using Proxy file

  1. Add a file called proxy.conf.json in your angular application at the same level where angular.cli.json/package.json resides.
  2. Add the content to specify where a request should be redirected to: For example, in the above code, any request with /Api1 is redirected to the ‘target’ specified in that particular object (http://<ipAdressOfApi1&gt;:<port number>) :
  3. In your package.json, add the following under “scripts”:

“scripts”: {

   “start”: “ng serve –proxy-config proxy.conf.json”


Start the app with the command Npm start and there you go!


  • Proxy.conf.json can act as load balancer too. You may redirect different requests to different servers without touching your code
  • Takes care of cross-origin browser requests issue.
  • Makes your application more maintainable as you don’t have to change the URLs if the URLs that host your APIs change frequently.

In production environment:


The above approach with proxy.conf.json cannot be reproduced in production environment. Even if it is tried with some hacks, it is largely discouraged. So, a better approach is to use open source NGINX.

NGINX can be used with major web service provides such as AWS.

  1. In the nginx.conf file find ‘server’ and add the following:
  2. Save the conf file and Restart NGINX

Your browser will be severing the content at the specified port in the NGINX

On other web service providers:

If you are not using NGINX, you must make proxy configuration in your hosting provider. For example, to do the proxy configuration in AWS, refer to the AWS doc:


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s