NodeJS REST plugin – Hapi & Co library for Generators

Recently we had to rewrite our NodeJS websocket implementation and turn it into a REST api.

1. Hapi – Hapi is a REST framework for NodeJS. It is very easy for use and we liked the idea that it can be very close to MVC. We chose it because it was faster than Restify and it is far simpler than it.

2. Co library – Co is a library which is designed to make writing code easier. When you write some code like accessing a database there are situations where you need to wait for the result to come. This is where you use promises. You can use libraries for promises like: Q, WhenBlueBird. We use When because it is simple. So you will use these libraries until the new ECMA script is approved and implemented (it has built in promises). So with Co you don’t need to use promises. You can use only the “yield” word and place the call to the function after it like “var result = yield getResult(10)”. The problem is that Co is working only with NodeJS version 0.11.+ which is still in development.

3. NodeJS version 0.11 – you can get it by installing nvm from the npm. “npm install nvm”. After that you can use “nvm install unstable” to install the latest unstable version and then you can list all of your NodeJS versions you have with “nvm ls”. After that you can choose one by typing “nvm use <version_name>” which you have from the nvm ls command.

Why we chose Co?

It is because we had a lot of code and we didn’t want to make our code filled with promises. They still don’t look like a great decision to have. And many of the database code like if you are using Mongoose most of the functions do not return Promises. You must manually return a promise when the Mongoose function finishes (which is one return new Promise but it is still a pain sometimes).

Mongoose

If you want to use Mongoose with Co and yield you must also use the development version of it.

Why migrating from Websockets to REST?

When we were starting our project I was the only one thinking REST is our solution. We have an api which gives ads to some mobile SDKs we have. We use websockets to get the ads and after each request we would close the connection to the server because it couldn’t maintain a lot of simultaneously opened connections. And we were NOT using it right. Our code became hard to maintain, a lot of field validation was missing and it was not very fast (websockets still have handshakes and stuff).

Why using NodeJS for REST?

This is what I ask myself every time I open that project. I really don’t like Javascript and write the code using CoffeeScript. But it does not support “yield” keyword so now everything should be rewritten into JS. And I am not very sure that NodeJS is the perfect REST solution. I am convinced at 100% that it is perfect for WebSockets. But I would write everything in Python and it would look like times easier to understand and simpler. But still the fact that it is interesting for me to see if NodeJS is good for REST makes me want to continue working on it.

CoffeeScript & Co – No

CoffeeScript cannot compile the “yield” keyword so you cannot use it with the Co library. Still, I do like CoffeeScript a lot. 🙂

You may also like...