RESTful Web Services: Getting Data

Welcome to Part 2 of this review of the Pluralsight course RESTful Web Services with Node.js and Express 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.

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

Getting Data

Implementing HTTP Get

Jonathan explains req is short for request and res is short for response. We’ll be using these conventions throughout the course.

We write a simple function to get the JSON

{ hello: “This is my api” }

Jonathan recommends Gildas Lormeau’s Chrome plugin JSONView, which allows us to validate and view JSON documents in Chrome.

Wiring up to MongoDB and Mongoose

Mongoose is to MongoDB what Entity Framework is to SQL Server. It’s an Object Relational Mapper.

npm install mongoose –save

Mongoose takes Mongo data and converts it into JSON objects that we use in our application.

var mongoose = require(‘mongoose’);

var db = mongoose.connection(‘mongodb://localhost/bookApi’);

There is a whole course on Mongoose for Node.js and MongoDB available on Pluralsight if you want to learn it in more depth, but hopefully this gives you enough to at least get started.

In this lesson we create models/bookModel.js and update app.js with a bookRouter MongoDB query.

We see that our query is returning all of our books in JSON.

Filtering with the Query String

We add a filtering option to our API so that we can filter on Genre.

This is done using the Express req.query method and checking that the genre exists

Getting a Single Item

Next we just send one id to get back a single result.

Book.findById(req.params.bookId, function(err,book) { … })

Part 3 – Posting Data is coming soon

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