Search code examples
angularjsnode.jsminifiedjs

client host name being appended to client requests


I have the following configuration in my angular nodejs app minified javascript file

   // Constants
    .constant('config', {
        appName: 'My App',
        appVersion: 1.0,
        apiUrl: "someAPI"
});

But when the requests are made from this client as

http:/server-hosting-client/someAPI/api/ Failed to load resource: the server responded with a status of 404 (Not found)

I want the requests to be sent as

 http://someAPI/api/

Where is this appending its own host name from?

Here is an example client request

.controller('HeaderCtrl', ['$scope', '$http', '$location', 'config', function($scope, $http, $location, config) {    
    $scope.appName = config.appName;
    $scope.selected = undefined;
    $http.get(
       config.apiUrl+'/api/', {
            'withCredentials' : true
        }).success(function(data) {



        });

Note that I am running the app from the dist folder

/dist/ npm start

Solution

  • config.apiUrl needs to include the protocol:

    // Constants
    .constant('config', {
       appName: 'My App',
       appVersion: 1.0,
       apiUrl: "http://someAPI"
    });
    

    Otherwise, the API url is assumed to be a sub-path of the current hostname.