2013-11-23

Today for my 30 day challenge, I decided to learn a Node.js module called restify.The restify module makes it very easy to write correct RESTful APIs in Node.js and provides out-of-the-box support for features like versioning, error handling, CORS, and content negotiation. It borrows heavily from Express (intentionally) as that is more or less the de facto API for writing web applications on top of node.js. In this blog post, we will develop a RESTful API for storing jobs. We will store the data in MongoDB.

Restify Prerequisites

Restify requires NodeJS and the NPM package manager which comes by default with node.js installations. You can download the latest version of NodeJS from official website. Once you have node.js and NPM installed, we will use the NPM system to install Harp.

This application uses MongoDB as data storage choice so please download the latest MongoDB release for your operation system.

Install Restify

Create a new directory at any convenient directory on your file system.

To install restify module, just type the following command.

We will use MongoJS as the MongoDB driver. Install the mongojs module, just type the following command.

Writing RESTful API

Now we have restify and mongojs installed, let’s write code. Create a new file called app.js file.

Copy and paste the following content to app.js.

The two lines show above load the restify and mongojs modules using the require function and assign them to variables.

Now, we will create a new server using restify API.

The code shown above creates a new server. The createServer() function takes an options object. We passed myapp as the name of the server in options object. You can view the full list of options in the documentation. After create the server instance, we call the listen function passing port, ip address, and a callback function.

Run the application by typing the following command.

You will see following on the command line terminal.

Configure Plugins

The restify module has a lot of built in plugins which we can use. Copy and paste the following in app.js. These should be added before the server.listen() function. Refer to documentation to learn about all the supported plugins.

The three lines shown above :

The restify.queryParser() plugin is used to parse the HTTP query string (i.e., /jobs?skills=java,mysql). The parsed content will always be available in req.query.

The restify.bodyParser() takes care of turning your request data into a JavaScript object on the server automatically.

The restify.CORS() configures CORS support in the application.

Configure MongoDB

Before we will add the routes, lets add the code to connect to myapp MongoDB database.

In the code shown above, we connect to local MongoDB instance. Next, we get the jobs collection using database object.

Writing CRUD API

Now, we have the server and database part ready. We still need routes to define the behaviour of the API. Copy and paste the following code to app.js.

The code shown above does the following:

When a user makes a GET request to '/jobs', then findAllJobs callback will be invoked. The another interesting part is the use of versioned routes. A client can specify the version using Accept-Version header.

When a user makes a GET request to '/jobs/123', then findJob callback will be invoked.

When a user makes POST request to '/jobs', then postNewJob callback will be invoked.

When a user makes DELETE request to '/jobs/123', then postNewJob callback will be invoked.

Now we will write the callbacks. Copy and paste the following to app.js.

The code shown above is self explanatory. We are using mongojs API to perform CRUD operations.

We can test the web services using curl. To create a new job, type the command shown below.

To find all the jobs

Deploy to Cloud

Before we deploy the application to OpenShift, we'll have to do few setup tasks :

Sign up for an OpenShift Account. It is completely free, and Red Hat gives every user three free Gears on which to run your applications. At the time of this writing, the combined resources allocated for each user is 1.5 GB of memory and 3 GB of disk space.

Install the rhc client tool on the machine. The rhc is a ruby gem so you need to have ruby 1.8.7 or above on your machine. To install rhc, just typesudo gem install rhc
If you already have one, make sure it is the latest one. To update the rhc, execute the command shown below.sudo gem update rhc
For additional assistance setting up the rhc command-line tool, see the following page: https://openshift.redhat.com/community/developers/rhc-client-tools-install

Setup the OpenShift account using rhc setup command. This command will help us create a namespace and upload your ssh keys to OpenShift server.

After setup, we can create a new OpenShift application by running the following command.

It will do all the stuff from creating an application, to setting up public DNS, to creating private git repository, and then finally deploying the application using code from my Github repository. The app is running here http://day27demo-{domain-name}.rhcloud.com//

That's it for today. Keep giving feedback.

What's Next

Sign up for OpenShift Online

Get your own private Platform As a Service (PaaS) by evaluating OpenShift Enterprise

Need Help? Ask the OpenShift Community your questions in the forums

Showcase your awesome app in the OpenShift Developer Spotlight. Get in the OpenShift Application Gallery today.

Show more