2

I have an AngularJS factory function like:

service.getUserRates = function() {
  return $http.get('https://localhost:8443/portfolio/get-user-rates');
}

This works fine on localhost, but before deploying on digital ocean servers I have to update the URL to include the IP address of the server - $http.get('http://67.205.xx.xxx:8443, instead of localhost or I get an ERR_CONNECTION_REFUSED error.

This is hard to manage since I am developing on local machine and pulling changes into cloud server, I have to keep changing the URL every time I do a new git push / pull.

What is the easiest / best way to manage this so I don't have to manually change the URLs repeatedly?

chuckieDub
  • 1,767
  • 9
  • 27
  • 46

2 Answers2

1

A simple solution that I've seen before is to replace all occurrences of

https://localhost:8443

with

$window.location.protocol + '//' + $window.location.host

You can always use an angular.module('yourAppName').constant to share the hostname around the entire application.

This has the added benefit of supporting subdomains out of the box. You don't have to change anything to deploy code to test.yourdomain.com or stage.yourdomain.com.

Sky
  • 372
  • 2
  • 7
0

Simply use root relative URLs:

 app.service("dataService", function() {
     this.getUserRates = function() {
         return $http.get('/portfolio/get-user-rates');
     }         
 });

In general, it is considered best-practice to use relative URLs, so that your website will not be bound to the base URL of where it is currently deployed. For example, it will be able to work on localhost, as well as on your public domain, without modifications.

— StackOverFlow: Absolute vs relative URLs

georgeawg
  • 48,608
  • 13
  • 72
  • 95