Intro to HapiJS [v16]

HapiJS or simply Hapi is a framework for NodeJS to create HTTP based applications; framework’s name explains its purpose in a much better way; H(TTP)-API

Intro to HapiJS [v16]

Intro to HapiJS
Hi All, Today we are going to have an intro to HapiJS. HapiJS or simply Hapi is a framework for NodeJS to create HTTP based applications.

In-fact the framework’s name explains its purpose in a much better way; HTTP-API.

Here is a Video on the intro to HapiJS

And following video from its creator Eran Hammer explains the vision behind the framework and its ecosystem.

Enough of the videos, Hapi gives preference to configuration over the code. There are various (or say numerous) endpoints to configure the server app which will give you a unified way to apply things in your application. The core and supplementary things which you need in your application are already there in the ecosystem.

Following guide is only suitable till version 16, v17 and onwards have breaking changes and hence following code examples may not work

Let see the code, the basic server with HapiJS.

To create a basic server with HapiJS, first import the lib, create a server instance for port, add the route, start the server

Creating the instance requires an object as parameter with at least one key named port. The server will listen on this port for incoming connections.

In above example, there is another key named host.

The key host is used only to set server.info.host and server.info.uri. If not configured, host key defaults to the operating system hostname and if not available, to localhost.


Routes

HapiJs routes are simple JavaScript Objects with few required keys and many optional keys to configure a route to its maximum extent.

A basic route object looks like following:

let route = {
  method: 'GET',
  path:'/hello',
  handler: function (request, reply) {
    return reply('hello world');
  }
}

This object needs to be passed to .route method of the hapijs server instance. This method accepts an array of route objects as well.

As you can notice the method key in above object is a string name of HTTP method. This key can also accept an array of HTTP method names e.g. method: ['GET', 'POST'], if you wanna handle all those method requests on any route path by same handler.

The handler methods are dead simple as they only accept request object and reply function respectively as parameters. The above route object can rewritten in following way as well, where handler is now a child key to config.

let route = {
  method: 'GET',
  path:'/hello',
  config: {
    handler: function (request, reply) {
      return reply('hello world');
    }
  }
}

The reply can simply be sent as reply(String|Array|Object);

Though reply is an api in itself and provide alternatives to handle a reply/response.

The behaviour of the reply function can be altered globally as well. Suppose you want to parse all requests' payload as text/plain by default to offload the parsing to each request handler; you can do it in following way:

server.connection({
  host: 'localhost',
  port: Number(process.argv[2]) || 8080,
  payload: {
    override: 'text/plain' 
  }
});

And similarly you can do so for other things of route like auth scheme etc. which can be read about here: https://hapijs.com/api#route-options

Other ways to configure the server can be:

Dynamic Routes

time2hack/hapi-demo/master/server-qs-dynamic.js

Serving Files

time2hack/hapi-demo/master/server-files.js

Handling File Uploads

time2hack/hapi-demo/master/server-file-upload.js

Please share your experience with HapiJS in comments.