Reputation: 1103
Say I'm implementing a frontend browser web application, connected to the backend via a REST API.
In development I'd like it to point to a API service at localhost.
In production I'd like it to point to a API service at another domain.
What is a good pattern to set the base domain used?
I was thinking of using server template rendering to determine if the base domain should be saved in a DOM or not. e.g. if !debug then show mydomain.com.
Not sure that's the greatest idea.
Upvotes: 0
Views: 111
Reputation: 2560
Since I don't know the details of your problem, there are 2 possible answers : the easy one and the hard one.
Easy :
Don't bother the domain. In your service, just declare the suffix of the URI you are trying to reach, and AngularJS will correctly call the service on the domain you're currently running :
var URI = "/rest/chapter/";
(...)
$http.get(URI).success(...)
Hard :
If the domain you are trying to reach is not the current one, then you're on the hard path.
The problem you are trying to solve is about "dev-ops", the link between developers and "operational" people, often solved on the dev side by rearing what is called "environment variable".
There is no straightforward answer for AngularJS, but server-side templating is not a very good option.
So if you google around for "environment variable" and "AngularJS", you already have some good ideas on Stackoverflow :
My opinion is that you should find a script that detect and replace the var URI according to the environment you're deploying.
Upvotes: 1