0

I have an angular 6 application running inside a Spring Boot app.

Is it possible to get parameters from the spring boot app inside the angular app?

Why? The spring app has a REST service that provide the config to the angular app (I cannot use the environment.ts file since the config is dynamic and will explain in a bit). But I do not know how to call the spring REST service from angular.

If I use http://localhost:80/MyRestURL the angular app will try to make a call to localhost but localhost from the browser will refer to the PC that I am launching the browser from. I need to go to the server that the app is running on.

Now I could go and grab the hostname from the url as described here.

let hostNameFromURL: string = this.window.location.hostname;
let correctLocalServerURL: string = "http://" + hostNameFromURL+ ":80/MyRestURL";

But if we launching the angular app from a load balancer the name in the URL would be the load balancer's URL and not the server that the Spring Boot App and Angular is running from. I could fetch the config via the load balancer but it seems counter productive to reference another machine (that might be unstable) if the user has already been routed to a server that works).

Is there another way to share data between angular and the spring boot app?

cp5
  • 1,087
  • 6
  • 26
  • 58
  • 1
    could you use relative paths instead of absolute? if the angular app is served from the same load balancer, that should give you the same result (i.e. `/MyRestUrl` instead of `http:://someServer/MyRestUrl`) – JoSSte Nov 13 '19 at 07:57
  • 1
    @JoSSte Thanks so much... Rookie mistake on my end. Mind adding it as an answer and i will accept. – cp5 Nov 14 '19 at 15:21
  • There you go @Chrispie – JoSSte Nov 14 '19 at 16:29

1 Answers1

2

You could use relative paths instead of absolute. If the Angular app is served from the same load balancer, that should give you the same result.

For example: if you page is hosted at http://www.someserver.com/ using http://www.someserver.com/MyRestUrl or /MyRestUrl as the path to your service will give the same result.

JoSSte
  • 2,953
  • 6
  • 34
  • 54