Hapi: The Good Parts

Recently, I've been working with a new framework called Hapi to build an API for Webmaker. This is a bit of a departure from the past, where we traditionally would have used Express to build the our server applications. The decision to use Hapi was based on several features that we found in our experimentation with the framework. I'd like to outline these features, and give examples about how we're using them.


We wanted our services to be highly testable. Hapi's Server API makes this a cinch. Its configuration-centric approach to building servers means you can split all of your configuration (like routes) into require-able modules that can be tested in isolation:

As you can see above, we can test the routes' configuration outside of actually building a running Hapi server. While the tests above don't cover situations where more configuration is added, you can use libraries like Joi to provide far more strict assertions on the configuration object.

One other key Hapi feature is it's inject function, which lets you simulate receiving a request. It is invaluable when testing, because it enables you to do very cool things like providing credentials that step over the authentication of your routes.


Hapi provides a plugin API, which makes separating your application into independent units very easy. This separation consequently makes testing really easy too. In your tests, you can register the plugin on a bare Hapi server, with whatever test specific configuration you desire, and test it's behaviour in isolation.

Server Methods

In the last gist I embedded, I added something called a server method. Server methods are a way to expose functions on your server object, which removes the need to require a common module everywhere a function is needed. Basically, if you define your server methods in a plugin, you register it once, and it's available everywhere!

Another really handy feature that server methods have is caching. Hapi is compatible with catbox, a multi-strategy key-value store, and Hapi leverages it for easy caching. This is extremely useful if the server method requests data from a database:


Hapi provides an interface for enforcing strict rules on the data coming into your application. This validation functionality works perfectly with the Joi library. It can be applied to route params (/foo/{bar}) request payload ({foo: 'bar'}) or query params (/foo/bar?fizz=buzz).

In summary, I'm very impressed with Hapi. With it (and with the help of a couple other great libraries called sinon and nock), I was able to achieve 100% test coverage on api.webmaker.org. All without having any external dependencies (other than PostgreSQL, but I can live with that, since the tests feel more real if they use an actual database)

Here's the part where I engage with you:

Do you use Hapi?

What do you think of it?

What are your favourite features or tricks when developing applications with Hapi?

edit: I didn't realize until this morning that a recent theme update disabled disqus. It's working now, should you wish to chat.