I have the SpringBoot application as BE and Angular as FE. SpringBoot is on port 8553 and Angular is on 4203. So for communicating, I'm using proxy.conf.json where I'm redirecting all the requests from 4203 to 8553 like this:
"/application": {
"target": "http://localhost:8553",
"secure": false,
"logLevel": "debug",
"changeOrigin": true
}
I'm starting the angular app with a proxy like this:
ng serve app --port 4203 --proxy-config proxy.conf.json --disable-host-check true
Everything is working fine. Now I want to run another SpringBoot instance on a different port 8554 and I want to use the same angular instance and switch between SpringBoot instances. I want to have a dropdown and user to pick one of two different SpringBoot instances, and angular to connect to that instance and proxy all the requests to that instance.
I have tried with two different proxy configs, but I don't know what to switch in runtime between them. Im not sure if this is even possible with my current setup.
I will also try using different environment files and switch them in runtime, if possible. This is more like an infrastructure and architecture problem, and I'm not very good at it :D
Assuming you mean Angular instead of AngularJS, follow these steps:
1. Update
proxy.conf.jsonAdd new proxy configs:
2. Create
ApiUrlServiceMake an injectable service
ApiUrlServicewith a propertyapiUrl = 'api1'.3. HTTP Interceptor
Create an HTTP interceptor to replace '/api' with
apiUrlService.apiUrl. InjectApiUrlServicein the interceptor.Don't forget to register the interceptor in your
AppModule:4. Switch API URL at Runtime
To change the API URL in runtime, modify
apiUrlvalue inApiUrlServicefrom a form.Inject
ApiUrlServicein your component:Use a select dropdown in your template to switch between '/api1' and '/api2' (remember to import the Angular
FormsModule: