In my Angular JS application, the backend is developed using .NET. To retrieve data, I make calls to the API controller methods from the .NET project.
When calling the controller methods, I have to prepend "" to the path of the method. Therefore, the complete URL looks like this:
http://localhost:port/api/controller/method
While this process seems straightforward, when transitioning the app to higher environments, I find myself manually updating this string with the respective server IPs. This means the same URL on a production environment becomes:
http://PROD-IP:port/api/controller/method
Despite the logic behind this approach, I have a couple of queries:
Shouldn't localhost automatically point to the server? If I deploy my app on 127.0.0.1 in every environment, then shouldn't localhost:port work seamlessly each time without needing to modify the IP for different environments? (This is my assumption and unfortunately, it is not effective)
Is there a way in JavaScript to extract the IP address from the current URL? This would allow users to enter the app URL initially, and then use the extracted server URL for all backend calls later on. I attempted to use
document.location
, but it did not yield the expected results.
I searched for solutions on several blogs, yet I couldn't quite articulate my issue properly and consequently, I didn't come across any suitable solutions.