//

INTRODUCTION TO HAPI

Created by Eran Hammer, hapi.js is an open source framework built on top of Node.js for building web applications and services by Wallmart. In this tutorial, we will go through the advantages of hapi and build a simple application using hapi as the server.

Why hapi.js?

1. Tested at Scale

Hapi.js is robust, stable and reliable. It was battle-tested during Walmart Black Friday without any problems. Major companies like Yahoo, Disney, Paypal, and Zappos use hapi.js for their APIs and web applications.

2. Security

Security is a key consideration behind hapi.js for building secure web applications.

3. Modularization

Hapi.js allows logical modularization of your application. It also has a rich plugin interface. Large applications can be easily distributed over multiple smaller modules.

Installing hapi.js

In order to use hapi.js, you have to make sure you have Node.js installed on your machine.

Installing Node.js

Download and install Node.js here.

In command line, run the npm install command for installing hapi:

npm install hapi --save

Creating a Server

Now we have installed Node.js, as a starting point, we are going to set up the server.

Create a file called server.js

var Hapi = require('hapi');

var server = new Hapi.Server();

server.connection({
  host: 'localhost',
  port: 3000
});

server.start(function() {
  console.log('Server running at:', server.info.uri);
});

Line 03 - Here a server instance is created. Line 05-08 - A connection is established by providing our server with a host and a port number. Line 10 - We are starting the server.

...

server.route({
  path: '/',
  method: 'GET',
  handler: function (request, reply) {
    reply('Hello world!');
  }
});

...

Routing

1.Path

The absolute path (Line 04) is used to match incoming requests (must begin with '/'). Incoming requests are compared to the configured paths based on the server router configuration option. The path can include named parameters enclosed in {} which will be matched against literal values in the request as described in Path parameters.

2.Method

The HTTP method (Line 05). Typically one of 'GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS'. Any HTTP method is allowed, except for 'HEAD'.

3.Handler

The handler for the route (Line 06). Can only include one handler per route.

HTTP method HEAD is not supported in the server route.

Now you can start the server by running following command:

node server.js

Plugins

Plugins allow you to build out your application components as self-contained modules.

They also manipulate requests and other events, hook into server events, etc.

Plugins are like middleware.

We can modify each request at the extension points, using server.ext(). Let's expand on our example code where we want to set a new request path values on all the incoming requests:

...

server.route({
  path: '/test',
  method: 'GET',
  handler: function (request, reply) {
    reply('This is a test!');
  }
});

server.ext('onRequest', function (request, reply) {
    // Change all requests to '/test'
    request.setUrl('/test');
    return reply.continue();
});

The Github source for this article can be found in here.