Rest API - My introduction to HapiJS

Until recently, if asked about the best framework and technology to create a Rest API I would have responded with Laravel Lumen.

I have used Laravel Lumen on a number of projects and always ended with solid results. With the opinionated nature of Laravel, it allows you to follow a somewhat set structure for your codebase. This is fantastic for bigger projects as I have found the codebase easy to maintain.

Recently however, HapiJS has been on the radar. It brings a very lightweight framework for drafting Rest API’s, boasting lightning performance stats and full customisability.

One of the best parts about HapiJS could also be thought of as a downside. This being the free reign given to the developer. As the developer, you can choose pretty much any way to structure your code. Meaning that jumping into an existing codebase could prove to be totally different to your last.

Still, with the performance benefits available. I saw a chance to experiment on a recent side project of mine. I wanted to create a slack integration which required an api to power a simple dashboard and post/receive data with the Slack API. So, this began my Hapi journey.

Disclaimer - This is not a guide, there are many other guides for getting started with HapiJS, the point of this post is to show you reasons for using it!

My first find was how little code is required to setup a the Hapi server. Ignoring setting up a package.json this is all you need:

var Hapi = require('hapi');

var server = new Hapi.Server();
    port : 4000,
    routes: {
    cors: true,
    validate: {
      options: {
        abortEarly: false

server.start(function() {
  console.log('Server is now running');

Now, obviously there would need to be more things like setting up the routes. But in essence, we now have a server which could be access with node ./index.js “index.js” being the name of the file.

With this simplicity comes speed, this allows you to hack together projects super quickly