Setting the Service's network address in config file

hey all,
I am writing an ionic application which makes REST api calls to GET, POST, etc. I want to externally set the IP EndPoint and use it everywhere. So if the service ip changes, I need not to change ip address in every $http. How to set the API’s EndPoint in config.xml? Help out guys…

I don’t know of any way that REST API address can be specified in config.xml. config file is for native settings and REST API calls are being made using JS. If you want flexibility regarding Server address then instead of hardcoding baseURL in in your $http use constants

angular.module(‘yourModule’,[]).constant(‘BASEURL’, ‘url’)

and inject it in the services where you make Server calls. Using Constants instead of hard coding things is a good practice.

You could try to put your address in app preferences.

Other simpler possibility: use a domain name rather than an IP address and let DNS do the work.

1 Like

this is nice!! Thank you…

Thanks gmarziou for your solution!

@saurabhgupta050, @gmarziou Is there is any way to specify the endpoint of the services in config. files and use it inside my app? I don’t want to disturb the code.Awaiting for your response!

You can achieve that in many different ways, one of which using gulp-ng-config
Refer to this article: http://atticuswhite.com/blog/angularjs-configuration-with-gulpjs/

Bare in mind that gulp tasks will be modifying your files when using ionic serve, given that you modified your ionic.project with the gulp tasks.

For ionic build, you would need to run the gulp task first then call ionic build.
Another solution is to use cordova hooks -> (https://blog.nraboy.com/2015/01/hooks-apache-cordova-mobile-applications/)

Generator-M-Ionic has very useful gulp taks to do it with angular constants and environments.