Sunday, October 1, 2017

Having trouble fetching json form node.js/express server using angular, probably url routing -(newbie)

I am in the process of making a game aplication using angular and phaser.io on the client side and node.js with express on the server side and have reached a stalemate in getting json from the server.

This is the angularjs call, prety simple,just get the json and read a parameter:

$scope.url = 'http://localhost:8080/game/usuario';
$scope.getInfo = function(){
             $http.get($scope.url).then(function(response) {
                $scope.mydistance = response.data.distance;
            });
         };

Then, this is the return function from the server.

app.get("/game/usuario", function(req,res){
console.log("JSON ACESSED!")
fs.readFile(__dirname + "/static/users.json", function (err, data) {
  if(!err) {
     res.end(data);
  } else {
     res.end(JSON.stringify(err));
  }

});

The console log “JSON ACCESED!” never fires when i call getInfo(), so i guessi’m doing something wrong on the urls.
Is worth noting that the aplication runs on the browser url http://localhost:8080/game/

and the htmls,images and js files are being served as static files.

Source: AngularJS



from Angular Questions https://angularquestions.com/2017/10/02/having-trouble-fetching-json-form-node-jsexpress-server-using-angular-probably-url-routing-newbie/
via @lzomedia #developer #freelance #web #lzomedia.com

No comments:

Post a Comment