charting progress
APIs can be frustrating - so much so that I sometimes need to walk away from a project for a day or two. The Internet Games Database API was no exception. Let's follow the logic!
Okay, so you've submitted a search term. First thing's first: let's sanitize that sucker.
We'll also need encodeURI(), since spaces aren't supported in query strings, which is how the API expects the search terms.
The extra parameters all come from the documentation. in this case, I'm filtering out games whose ratings are not greater than 0 (ie, don't have a rating); ordering by popularity, so we're more likely to get legitimate results; and setting the limit to 50, which is (sadly) the maximum. Let's execute that request:
When we get a response, I know it'll be an array-like string, which we'll .map() down to game ids, sending out a second request for all their info.
Note that I'm attaching these game ids to the request variable, and passing that, along with the callback function, into the next step. This keeps everything clean, since every function knows to expect the full request and the “final” callback, which will send the response.
Okay, same story here. What's happening .on("end") now? What do we do with the data?
First, we make a simple list of all the words you originally searched for - this lets us .filter() out any results that don't contain the actual search terms.
Second, we clean up the otherwise complex data. To make those pretty graphs, we only need a few parameters, though the JSON from the API may or may not have them, and might even call them something different.
Third, we .filter() out games that don't have both a rating && a date, since both are necessary to plot the point on the graph.
And finally, we .sort() the games chronologically, then return them back up to trigger the callback function, which sends the response client-side for visualization. ...What games are you gonna look up?
tl;dr: this story glosses over the struggles of API authentication and error handling; third-party data is sloppy, so make sure to clean it up and organize it; making a request from nodeJS looks a lot like responding to one
project: GamesTimeline











