Completely new to Node.js - API Programming


I've picked up Node.js recently. Before I jump to Express.js and other web frameworks, I learned few things about them but I wanted to try some API programming. I downloaded this module: https://www.npmjs.com/package/steamwebapi

Since I'm new to Node.js, I made new javascript file, app.js and my code is:

var SteamWebAPI = require('steamwebapi').SteamWebAPI;

SteamWebAPI.setAPIKey('My key is here');

SteamWebAPI.getRecentlyPlayedGames('76561198190043289', 5, function(response) {
    console.log(response);
});

SteamWebAPI.getRecentlyPlayedGames('76561198190043289', 5, function(response) {
    console.log(response);
});

'76561198190043289' is my steam 64 id. When I type: node app.js in terminal, I get:

{ response: { total_count: 1, games: [ [Object] ] } }
{ response: { total_count: 1, games: [ [Object] ] } }

How do I display my results, what I'm doing wrong?



You are not doing anything wrong. console.log makes the output more compact for the sake of readability. It can get very lengthy at times so it's probably a good thing.

It means that "[ [Object] ]" is actually an array of one or more games. You can try using

console.dir( response );

instead, or you can be more specific with log:

console.log( response.response.games );.

There are other ways around this as well if you can bother to search around. Converting to a string seems to be popular:

 console.log( JSON.stringify( response, null, 4) );

Off topic...

I would also like to mention something else (since you are new) the one thing everyone must relearn coming to node from js. Calling functions like you do:

function1(..., callback);
function2(..., callback);

Node moves on to the second function immediately without waiting for the first callback to finish. So you have no idea of which of those functions will finish first. To force the order you would have to do this:

SteamWebAPI.getRecentlyPlayedGames('76561198190043289', 5, function(response) {
    console.log(response);
    SteamWebAPI.getRecentlyPlayedGames('76561198190043289', 5, function(response) {
        console.log(response);
    });
});

Your code will turn into the infamous callback hell. There is no avoiding it. It will happen sooner or later! To prevent that, learn how to use promises. You'll be much better off going from there!


Edit: Connect to angular

You need to create a server-backend of some kind. This is how it could look like using express (since you mention express):

var express = require('express');
var server = express();
var SteamWebAPI = require('steamwebapi').SteamWebAPI;
SteamWebAPI.setAPIKey('My key is here');

// define endpoints
server.get('/games', function (req, res) {
    SteamWebAPI.getRecentlyPlayedGames('76561198190043289', 5, function(response) {
        res.json(response.response.games);
    });
});

// Start the server
server.listen(3000, function () {
  console.log('Example app listening on port 3000!');
});

Once it's running you can test that it works by browsing to http://example.com:3000/games

Then you call your endpoint from angular:

var app = angular.module("app", []);
app.controller("myCtrl", function($scope, $http){       
    $scope.games = [];
    $http.get('http://example.com:3000/games').then(function(response){
        // The response is seldom exactly what you expect. Check your browser console.
        console.log(response); 
        $scope.games = response.data;
    });
});

html

<body ng-app="app" ng-controller="myCtrl">
    <div ng-repeat="game in games">{{ game }}</div>
</body>

All done.

Mind you, this is all shooting from the hip (no testing whatsoever) , so there are bound to be errors. But it should give you a starting point at least.