17

In Angular (Type Script) there are many config files. Which is the right one to save a global setting?

For example, when I am calling API from local then my rootUrl is localhost:42000 but when I switch on production it should be http:www.someting.com.

I want to save this rootUrl in some global place so if I switch on production then I only have to change in this rootUrl.

Please suggest where should I save these global settings, same as web.config in Asp.Net.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Umang Patwa
  • 2,795
  • 3
  • 32
  • 41
  • 1
    angularjs and angular 4 **are not the same framework**. which are you using? one is javascript, the other is typescript. – Claies May 24 '17 at 06:06
  • I am using angular 4 – Umang Patwa May 24 '17 at 06:07
  • 1
    @UmangPatwa its angular not angular js please change – Rahul Singh May 24 '17 at 06:08
  • 2
    Possible duplicate of [Define global constants in Angular 2](https://stackoverflow.com/questions/34986922/define-global-constants-in-angular-2) – ippi May 24 '17 at 06:08
  • i think he using TypeScript one – Maher May 24 '17 at 06:08
  • 1
    If the URL that serves your app is also the URL that serves your API, then you don't need any rootUrl. Just use http.get('/api/blabla'). Otherwise, Angular CLI has environments for that kind of stuff. But all you need is an exported constant wherever you want, that you import wherever yo need it. – JB Nizet May 24 '17 at 06:09
  • Do we need to build project two times with prod and development, or is there any other way to change the root URL of API pointed on different server and root url change without building the project. – AmitykSharma Nov 21 '19 at 09:58

4 Answers4

12

This answer is similar to @trichetriche, with few more details on the code.

For development/testing purpose

environment.ts

export const environment = {
  production: false,
  appUrl: 'localhost:4200'
};

For production

environment.prod.ts

export const environment = {
      production: true,
      appUrl: 'mywebsite.com'
    };

Usage

service.ts

import { environment } from '../../environments/environment';

this._http.get(environment.appUrl, requestHeaders(options));

Make a note of production parameter in all the environment files. I believe you could also create more environments like environment.unittesting.ts.

Amit Chigadani
  • 28,482
  • 13
  • 80
  • 98
4

When I first started using Angular 2, I used a global.ts file where I'd put all my variables, so that I could change it easily.

I've then discovered the environments provided by angular CLI. All you have to do is name a file environment.prod.ts (for prod), and use ng build --prod when you build it. when you develop, use the environment.ts file and both files have to have the same variables.

I hope this answers your question.

2

I have another way to define global settings. Because if we defined in ts file, if build in production mode it is not easy to find constants to change value.

export class SettingService  {

  constructor(private http: HttpClient) {

  }

  public getJSON(file): Observable<any> {
      return this.http.get("./assets/configs/" + file + ".json");
  }
  public getSetting(){
      // use setting here
  }
}

In app folder, i add folder configs/setting.json

Content in setting.json

{
    "baseUrl": "http://localhost:52555"
}

In app module add APP_INITIALIZER

 {
      provide: APP_INITIALIZER,
      useFactory: (setting: SettingService) => function() {return setting.getSetting()},
      deps: [SettingService],
      multi: true
    }

with this way, I can change value in json file easier.

I applied this in project for baseUrl, dateformat, sessiontimeout...

Hien Nguyen
  • 24,551
  • 7
  • 52
  • 62
0

You can use process.env with webpack quite easily. E.g.

/**
 * This interface makes sure we don't miss adding a property to both `prod` and `test`
 */
interface Config {
  someItem: string;
}

/**
 * We only export a single thing. The config.
 */
export let config: Config;

/**
 * `process.env.NODE_ENV` definition is driven from webpack
 *
 * The whole `else` block will be removed in the emitted JavaScript
 *  for a production build
 */
if (process.env.NODE_ENV === 'production') {
  config = {
    someItem: 'prod'
  }
  console.log('Running in prod');
} else {
  config = {
    someItem: 'test'
  }
  console.log('Running in test');
}

and you can change process.env.NODE_ENV using webpack -p --define process.env.NODE_ENV='\"production\"' --config ./src/webpack.config.js

More

https://basarat.gitbooks.io/typescript/docs/tips/build-toggles.html

basarat
  • 261,912
  • 58
  • 460
  • 511