Rezwan
Rezwan

Reputation: 41

How to connect Angular application with more than 1 server?

I have more than 1 server running on different ports.

  1. http://localhost:8081
  2. http://localhost:8082
  3. http://localhost:8083

Currently, I can connect all modules/library of my Angular 2 application to any single server.

However, how to connect different library with different servers?

I am following micro-service approach for Angular application following this article,

Upvotes: 1

Views: 519

Answers (2)

Rezwan
Rezwan

Reputation: 41

Currently, we are using the micro-frontend approach to connect with different services. https://micro-frontends.org/

Upvotes: 2

Lucho
Lucho

Reputation: 1547

I would suggest you to take use of environment variables that you inject into your services (assuming you have one for each service lib) each one with a specific variable and once done you can run the cli with(depending what version of the cli you running):

ng build --env

or

ng build --prod --env=prod

with minified production code.

and in your env files:

environment.ts:

export const environment = {
  production: false,
  libOneUrl: 'http://localhost:8081',
  libTwoUrl: 'http://localhost:8082',
  libThreeUrl: 'http://localhost:8083',
};

and in your service.ts inject it by:

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

apiUrl = environment.libOneUrl;

for dev resp. prod build do same in the files environment.dev.ts resp. environment.prod.ts (set production variable to true for prod build).

Upvotes: 2

Related Questions