Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different proxy config based on environment in Angular 2 CLI

How can I declare 2 different proxy URLs for development and production environments in Angular 2 CLI project? For example, while in development mode, I would like to use

{
    "/api/*": {
        "target": "http://localhost:3000",
        "secure": false
    }
}

but in production mode, I will use

{
    "/api/*": {
        "target": "http://api.exampledomain.com",
        "secure": false
    }
}
like image 271
Tu Shams Avatar asked Aug 05 '17 18:08

Tu Shams


People also ask

How do you create an Angular app once and deploy it to multiple environments?

All we have to do is: Add a JSON configuration file in the src folder. Update our angular/webpack configuration to include the file in our dist folder. Add a simple configuration service with a call to get our config data from our config file.

What is proxy config in Angular?

Angular CLI uses webpack-dev-server as the development server. The webpack-dev-server makes use of the powerful http-proxy-middleware package which allows us to send API requests on the same domain when we have a separate API back end development server.

What is the use of proxy config JSON?

conf. json" }, You can use the proxying support in the webpack dev server to divert certain URLs to a backend server, by passing a file to the --proxy-config build option. For example, to divert all calls for http://localhost:4200/api to a server running on http://localhost:3000/api, take the following steps.

What is environment TS file in Angular?

A project's src/environments/ folder contains the base configuration file, environment.ts , which provides a default environment. You can add override defaults for additional environments, such as production and staging, in target-specific configuration files.


2 Answers

in fact, you can do it, you just need to configure the router :

{
    "/api": {
        "target": "https://api.exampledomain.com",
        "secure": false,
        "logLevel": "debug",
        "router": {
          "localhost:4200" : "http://localhost:3000/exampledomain",
          "staging.exampledomain.com" : "http://api.staging.exampledomain.com"
        }
    }
}

What this do :

  • yout url match the proxy => will call the target defined
  • the host url match one router configuration => use the new target

For example :

I've deployed on localhost:4200 my angular app, when calling the url "api/callMeMaybe", then the router detect it and redirect in "http://localhost:3000/exampledomain".
If I've been on staging.exampledomain.com then the redirection will have be to "http://api.stagging.exampledomain.com".
Then, if none match, it keep the original target redirection.

Be careful, as order matters (the 1st match will be take)

Here is the documentation with an example

Edit

You can find the host value on your chrome debugger Network tab and selecting the api call, you get this details : api call header screenshot

like image 161
LE GALL Benoît Avatar answered Oct 25 '22 03:10

LE GALL Benoît


I do not believe you can control the proxy feature through the environment files. An alternative could be to define your api domains in your environment files

// environment.ts
export const environment = {
    production: false,
    api: 'http://localhost:3000'
};

// environment.prod.ts
export const environment = {
    production: true,
    api: 'http://api.exampledomain.com'
}

then in your ts source files pull the domain from the environment file

// some service
import { Injectable } from '@angular/core';
import { environment } from '../../../environment.ts';
import { Http } from '@angular/http';

@Injectable()
export class SomeService {
    constructor(private http: Http);

    getData(){
        return this.http.get(environment.api + '/rest-of-api');
    }
}

now when you run your build or serve commands they will use the api path defined in the environment file

like image 30
LLai Avatar answered Oct 25 '22 04:10

LLai