Comparing Express, Restify, hapi and LoopBack for building RESTful APIs

Editor’s Note: This post was originally published in April, 2014. We have refreshed this popular blog post.

If you are writing a Node.js application, chances are you going to have some kind of API endpoints to be consumed by your front end or expose data for others to take in. This is where RESTful APIs come in. With so many tools and approaches to choose from, you have a dilemma: What’s the right approach for your project?

Thanks to the incredibly active Node.js community, the amount of results for a rest search on NPM is pretty overwhelming. Everyone has their own implementation and approach, but few seem to agree on a common way to go about implementing RESTful APIs in Node.js.

RESTful APIs with Express

The most common approach is to just roll your own end points with Express. This practice allows you to get started quickly, but it becomes burdensome in the long run. Lets look at pros and cons:

Example

Here’s what a typical end point might look like in Express using the latest 4.x Router feature:

varexpress=require('express');varItem=require('models').Item;varapp=express();varitemRoute=express.Router();itemRoute.param('itemId',function(req,res,next,id){Item.findById(req.params.itemId,function(err,item){req.item=item;next();});});// Create new ItemsitemRoute.post('/',function(req,res,next){varitem=newItem(req.body);item.save(function(err,item){res.json(item);});});itemRoute.route('/:itemId')// Get Item by Id.get(function(req,res,next){res.json(req.item);})// Update an Item with a given Id.put(function(req,res,next){req.item.set(req.body);req.item.save(function(err,item){res.json(item);});})// Delete and Item by Id.delete(function(req,res,next){req.item.remove(function(err){res.json({});});});app.use('/api/items',itemRoute);app.listen(8080);

Pros

Cons

All endpoints need to be created manually; you end up doing a lot of the same code (or worse, start rolling your own libraries after a while).

Every endpoint needs to be tested (or at the very least I recommend that you hit the endpoints with HTTP consumer to make sure they are actually there and don’t throw 500s).

Refactoring becomes painful because everything needs to be updated everywhere.

Doesn’t come with anything “standard”, have to figure out your own approach.

Express is a great starting point, but eventually you will feel the pain of “roll your own” approach.

RESTful APIs with Restify

Restify is a relatively old player in the Node.js API field, very stable and being actively developed. It is purpose-built to enable you to build correct REST web services and intentionally borrows heavily from Express.

Cons

The cons are all the same with Restify as they are with Express; lots of manual labor.

RESTful APIs with hapi

hapi is a lesser-known Node.js framework that is getting momentum thanks to full-time support of the Walmart Labs team. It takes a somewhat different approach from Express and Restify by providing significantly more functionality out of the box for building web servers.

Once your API is ready for prime time, you can easily import your StrongLoop projects into API Connect to build an API that’s ready for the rigors of enterprise-level service. API Connect combines IBM API Management and StrongLoop into a unified API creation tool. It takes just a few simple steps to get started!