JavaScript Node Example

Node is a way to use the JavaScript language to run scripts or host a web server. Board Game Atlas's server runs on Node and many of the site features and scripts we use are written using Node as well.

Here's a simple example of how to start using the API in NodeJS right away.

Follow Along

If you'd like to get this code up and running yourself then use the following steps to do so. It takes < 5 minutes.

  1. Install NodeJS.
  2. Create a file name gameNameFetcher.js
  3. Copy and paste the code in the example below into the file.
  4. Open your Command Promt (Windows) or Terminal (Mac/Linux)
  5. cd into the folder that contains gameNameFetcher.js
  6. run npm install request
  7. run node gameNameFetcher.js

FilegameNameFetcher.js

var request = require('request');

request({ url: "https://api.boardgameatlas.com/api/search?order_by=rank&client_id=" } , function(err, res, jsonString) {
    var json = JSON.parse(jsonString);
    var gameNameList = json.games.map(e => e.name);
    console.log(gameNameList);
});

Explaination

We make a request to get https://api.boardgameatlas.com/api/search?order_by=rank&client_id= which returns use a list of 30 games ordered by their rank. Then we do some simple manipulation with the returned data.

  1. Convert the returned jsonString to a json object
  2. For each object in the games array just return their name using map()
  3. Print out the names

ConsoleOutput

$ node gameNameFetcher.js
$ ['Catan', 'Carcassonne', 'Pandemic', '7 Wonders', 'Ticket to Ride', 'Codenames', 'Agricola',...23 more]