Node.js: Structure and 3rd Party APIs

Welcome to the part of this review of the Pluralsight course Building Web Applications with Node.js and Express 4.0 by Jonathan Mills.


Jonathan is a JavaScript and Node.js expert working mostly in the MEAN Stack with individuals and companies to help build their technical skills to cope with the constantly changing landscape of software development.

He is also an ASP.NET insider and an international speaker focusing on JavaScript both in the browser and on the server.

In this course we’re building a library application that’s going to keep track of a list of books. And in this module we learn how to create users, and do authentication and authorization using Passport.

Also in this series:

Part 1 – Getting Started
Part 2 – Setting up Express
Part 3 – Setting Up Gulp
Part 4 – Templating Engines
Part 5 – Routing
Part 6 – Databases
Part 7 – Authentication

In this module we call out to a website called Goodreads to pull in book information.

Structure and 3rd Party APIs


Our bookRoutes.js is hard to read. We can refactor the code using controllers.

We create a controllers folder with bookController.js.

The code in here uses the revealing module pattern.

Our earlier get function becomes a getById function in our controller.

One advantage of the revealing module pattern is that we can look at the bottom of the file and immediately see what all of the most important functions in our file are.

For details on several JavaScript design patterns you can see Dan Wahlin’s Structuring JavaScript course.

Jonathan also has a popular course Practical Design Patterns in JavaScript.

Goodreads API

Details of the goodreads API can be found at

We want to fetch the War and Piece book image and other details so that we can display it on our site.

We first apply for a developer key.

Next we will call and fetch the data in XML format.


We create Services/goodreadsServices.js with a getBookById function.

In our bookController.js getById function, above the render part of the code, we call our new bookService.getBookById.

We also need to update bookRoutes.js, requiring in our goodreadsService.

Finally we make use of the revealing module pattern again in goodreadsService.js to expose our getBookById function.


Goodreads sends us the data in XML, but we want to work with JSON instead. There’s a handy package called xml2js that was written by Marek Kubica.

This package creates a parser and we get a reference to this:

var parser = xml2js.Parser({explicitArray: false})

In our getBookById function we create an options object and a callback function which uses the parseString function on our parser.

As HTTP response data comes back we append it to a string. We send that string to our XML parser, and then call the callback.

We have our book description rendering correctly in our website, and next we want to display the image.

In bookView.js we replace the URL of the random face with <>

Adding the id

One last step and we are done!

In adminRoutes.js we have an array of books. In here we add bookIds for War and Peace and Les Miserables.

On the command line we do db.books.remove({}) to remove all of our books from MongoDB.

Then we go to our addBooks URL to repopulate them.

It’s just a simple bit of code in bookController to call our bookService when we have a bookId.

And it works and we see the War and Peace and Les Mis images are now coming from good reads!

That’s all folks! Thanks to Jonathan for creating this course, and thanks to you for reading.

The next course that I’ll be reviewing for you is Shawn Wildermuth’s Large Scale JavaScript on Client and Server. And then we’re back with Jonathan again and RESTful Web Services.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s