Welcome to the final part of this review of the Pluralsight course RESTful Web Services with Node.js and Express by Jonathan Mills.
Node.js and Express gives us the ability to write lightweight, fast, scalable APIs quickly.
Also in this series:
Part 1 – What is REST?
Part 2 – Getting Data
Part 3 – Posting Data
Part 4 – Updating Data
Part 5 – Testing
In this course we have built out all of our HTTP verbs, and written unit and integration tests. The last remaining item is HATEOAS: Hypermedia As The Engine Of Application State
The Problem Around Navigating APIs
We currently provide a hexadecimal id when we only want a single book. The technique that has been used so far is to copy the id retrieved for the complete book list results, and paste it into the URL.
Customers would not be able to intuitively use this API.
HATEOAS aims to self document your API with hypertext links.
Adding Hypermedia to Our API
In our bookController we forEach through each of our books.
Jonathan explains that we need to create a new book and copy the element over inside this loop. In here we add a new links object and this contains a child object called self.
self is the URL that we use to fetch the resource.
Now if we run the API when we see the full list of books we can just click a link to move to the singular book view. This is much easier to use.
Next we update bookRoutes.js with a get method for our singular book route. This includes links.FilterByThisGenre.
When we run it again we can click the FilterByThisGenre link to all of the books that have the same genre.
There’s a problem with the “Historical Fiction” genre because the space character needs to be URL encoded to %20, but that’s a simple fix.
And that concludes the course!