Getting Started with Hapi 8

RisingStack's services:

Sign up to our newsletter!

In this article:

Hapi is a rich framework for building applications and services

hapi framework

hapi enables developers to focus on writing reusable application logic instead of spending time building infrastructure.

hapi is currectly being used by companies like Walmart (not just used but actively developed and maintained), Yahoo, PayPal or Mozilla – even the new npmjs website is built using it.

Starting your first server

Before dive into it, make sure that your are using version 8 of hapi, as some API calls are different in previous versions.

Installing hapi

You can grab the latest version of hapi from NPM:

npm install hapi --save

Launch the hapi-ness

Let’s take a closer look on what is happening here!

  • First, we create a new hapi instance.
  • Then our connection is defined – it is important, because in hapi you can define more, not just one. It can be useful when you want to create an API and a Web app as well in the same project, one listening on port 8001, the other one on port 8002.
  • The third step was to define a route handler. A route handler has a request and a reply argument, the first one contains information on the incoming request, while with the reply we can tell hapi how to respond to them.
  • Lastly, we start our server with server.start

Diving deeper

To be able to use hapi’s full power we have to understand some of the key features/mechanisms of hapi:

  • lifecycle of a request
  • plugins
  • server methods

Lifecycle of a request

hapi enables us a very granular control over incoming requests. The following happens to an incoming request:

Hapi request lifecycle

We can modify each request at the extension points, using server.ext(). Let’s take a look at an example when we want to set a new request path values on all the incoming requests:

Plugins

In hapi plugins make it really easy to break your application up into isolated small applications with seperate business logic. Ideally all your application code goes to plugins, your server should only provide configuration to them.

Writing a plugin

Writing plugins is very simple – you only have to implement a register function with the following signature: function (server, options, next):

Register takes three arguments: serveroptions and nextserver is a reference to our server instance, so here we can add route handlers as well, or access server methods that will be discussed later on. The options is a plain JavaScript object that the user passes to the plugin.

After all done configuration is done we have to call next to signal hapi once we have finished registering our plugin – this can be useful also if you do some async operation in your application’s bootstrapping phase.

Load plugings

To be able to use the previously created plugin we only have to load it and start the server:

Easy, huh? 🙂

If you want to register more plugins at the same time, then you can pass an array of plugins to the server.register.

Configuration

If you want to pass a configuration to the server and want to access it in every plugin you can do the following when creating a hapi server:

After that this configuration object will be accessible on the server object using server.settings.app – yes, even in your plugins!

Server methods

Server methods can be used to share functions by attaching them to the server instance. You can add a server methods like this:

Later on this function can be accessed on the server object using server.methods.twitter.fetch – again, in your plugins as well, as you can register server methods in plugins too!

Next

The features and mechanisms covered in this post hopefully get you interested in start using hapi.

For a complete API reference check out the GitHub repository.

Check out our guide: learn how to use generator functions with Hapi.

Share this post

Twitter
Facebook
LinkedIn
Reddit