0

During development, my server is running on localhost. So my code is:

this.http.post ('http://localhost:8000',body).subscribe(...

But after deployment, my server is running on another IP (e.g 128.3.130.61) This remote PC contains also the 'dist' folder.

I want the address in http.post to be always the server's one.

How can I do it without running "ng build" ?

Thank you, Zvika

Zvi Vered
  • 459
  • 2
  • 8
  • 16
  • Does this answer your question? [Angular CLI: Change REST API URL on build](https://stackoverflow.com/questions/47426721/angular-cli-change-rest-api-url-on-build) – Shabbir Dhangot Dec 13 '19 at 07:09
  • Read https://github.com/angular/angular-cli/blob/master/docs/documentation/stories/proxy.md and https://github.com/angular/angular-cli/wiki/stories-application-environments – JB Nizet Dec 13 '19 at 07:10
  • window.location.origin will return ip:port – Zvi Vered Dec 13 '19 at 07:13

1 Answers1

1

Use the environment files to manage these URL's. There will be two files environment.prod.ts and environment.stage.ts under environments folder which can be used to dynamically change these URL's.

environment.ts

export const environment = 
{
    production: false,
    API_URL: 'http://localhost',
};

environment.prod.ts

export const environment = 
{
    production: true,
    API_URL: 'https://128.3.130.61',
};

Import this environment in your respective service and use it accordingly.

app.service.ts

import { environment } from '../../environments/environment';
private API_URL= environment.API_URL;

And during production, mention the command as

ng build --env=prod

Hope it helps!

Suhas Parameshwara
  • 1,344
  • 7
  • 15