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.
- Install NodeJS.
- Create a file name gameNameFetcher.js
- Copy and paste the code in the example below into the file.
- Open your Command Promt (Windows) or Terminal (Mac/Linux)
-
cd
into the folder that contains gameNameFetcher.js - run
npm install request
- 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.
- Convert the returned jsonString to a json object
- For each object in the games array just return their name using map()
- Print out the names
ConsoleOutput
$ node gameNameFetcher.js
$ ['Catan', 'Carcassonne', 'Pandemic', '7 Wonders', 'Ticket to Ride', 'Codenames', 'Agricola',...23 more]