Node.js and REST

September 24, 2014

Blog | Development | Node.js and REST
Node.js and REST

In my last post, I wrote a front end that consumes a simple REST API endpoint and utilizes http verbs in an idiomatic way. Today, we’ll build a simple back end using Node.js and some tools. Remember: the objective is to understand what makes a REST API powerful and flexible.

Preparation

First, install Node.js.The folks over at Joyent provide great instructions on their website if you need assistant.  Node supports most operating systems and comes bundled with the Node Package Manager, or NPM for short, which is an indispensable tool for managing dependencies in whatever software you write.
Next, install the Sails.js frame work which will make it easier to prototype a REST API. To do this, you’ll use NPM with the command:

npm install sails –g


If you’re on a Mac or Linux system, you might need administrator privileges. Here’s the command:

sudo npm install sails –g


Next up, tell Sails to create a new application. For this tutorial,  call it “sailsAPI”:

sails new sailsAPI


Sails will create a folder and a Node application. Navigate into that folder, then tell Sails to create the scaffolding for a new model and controller called “foo”:

cd sailsAPI
sails generate api foo


If you inspect the files, you’ll see how Sails uses the MVC pattern. Views are in a folder in the root of the application, but controllers and models are in a folder called “api.” This occurs because Sails is slanted toward REST APIs. The Sails.js site has a some very helpful documentation if you want to learn more about that.
Now, let’s look at the foo controller in the “controllers” folder. It doesn’t have much code in it, but we’ll circle back to that.

/**
 * FooController
 *
 * @description :: Server-side logic for managing foos
 * @help        :: See http://links.sailsjs.org/docs/controllers
 */
module.exports = {
 
};


Getting Underway

To run the Sails application, simply enter the command:

sails lift


Open a browser to http://localhost:1337 and bask in the glory of scaffolding. Though we haven’t really done much, there is a lot of structure, including a default “home” view with an accompanying controller. Plus, when we created the “foo” model and controller, Sails gave it a lot of functionality without forcing us to write code.
The sample code we wrote in “Why REST” can talk to this API to create, read, update, and destroy “foo” items. (And that’s without us writing any extra code, or inventing a protocol!) This happens because the Sails framework assumes you want to use every controller you create in the idiomatic RESTful way: it automatically creates handlers for GET, PUT, POST, and DELETE on every controller and binds it to a model, and it follows the pattern I discussed last time:

GET http://localhost:1337/foo       -- lists all foo objects
GET http://localhost:1337/foo/{id}  -- gets one specific foo object
POST http://localhost:1337/foo  {some json form data} -- creates a foo object
PUT http://localhost:1337/foo/{id}  {some json form data} -- updates one specific foo object
DELETE http://localhost:1337/foo/{id}  -- deletes one specific foo object


In doing this, we create a generic and usable REST API with very little custom code. That makes mocking up a front end that talks to an API incredibly easy. In the real world, however, you would never put a naive implementation like this into production. You would override the methods as needed, lock down ones that the application won’t be using, and add layers of security.

BTW, if you don’t already have it, install Google Chrome and the Postman Extension. Postman let’s you “speak http,” or rather, talk directly to the REST API. You’ll have access to the full complement of verbs (GET, POST, PUT, DELETE, OPTIONS, etc) and you’ll be able to craft the contents of the message. Best of all, it’s in JSON, just the way JavaScript likes it.






The working code for this example is over at Bitbucket, so check it out. If you have comments or questions, tweet to us @GeekHive. Otherwise, I’ll see you next time when I implement the same REST API in Microsoft’s Web API framework.

 

Dan Clouse

Senior Developer
Tags
  • Node.js
  • Patterns & Practices
  • Tutorial

Recent Work

Check out what else we've been working on