0

I have written REST API using spring mvc which is hosted on one domain and Front end is written using HTML and angular JS which is hosted on another domain.Now I want to call REST API from another domain on which Frond UI is placed so how can I call REST API using relative URI instead of absolute URI?

For e.g.Instaed of

webbrokerModule.factory('CustomerContact', [ '$resource', function($resource) {
    return $resource('http://localhost:24000/webbroker/customercontact/:brokerid', {
        brokerid : '@brokerid'
    }, {
        get : {
            method : 'GET'
        }
    });
} ]);

I want to call it as 
webbrokerModule.factory('CustomerContact', [ '$resource', function($resource) {
    return $resource('/webbroker/customercontact/:brokerid', {
        brokerid : '@brokerid'
    }, {
        get : {
            method : 'GET'
        }
    });
} ]);

I do not want to mention IP address and PORT.

1

1 Answer 1

1

You need to create a factory or a service of hardcoded resources.

For instance :

angular
.module('app')
.factory('storage', storageFactory);

function storageFactory() {

var r =
{
    serverAdress : 'https://server.address.fr',
    //serverAdress : 'https://another.server.adress.fr',
    //serverAdress : 'http://localhost:3000',
};

return r;

}

Example of usage :

$http.get(storage.serverAdress + '/relative/path').then(function(res)...

This way you can encapsulate all your hardcoded data and change them more easily depending on your Environment variables (for instance if you want a different serveur address for the test server or something).

It is also very simple if one day you want to change your domains. This kind of practice is called a thin layer.

Sign up to request clarification or add additional context in comments.

3 Comments

Is there anything like bootstraping application which will allow me to use relative url.right now i have 3 different environmemt so i need to have 3 different file as per your logic
Nope, not at all, just change the serverAddress in the js file at runtime if you are in a different environment
Just check how tu use process_env variables and use that var instead of hardcoding serverAdress is the file. This way the serverAdress in the file will always refer to a linux environment variable. Your job to declare it in your run script when launching in the environment, but that is pretty easy :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.