Hapi is a rich framework for building applications and services
hapi enables developers to focus on writing reusable application logic instead of spending time building infrastructure.
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.
You can grab the latest version of hapi from npm is a software registry that serves over 1.3 million packages. npm is used by open source developers from all around the world to share and borrow code, as well as many businesses. There are three components to npm: the website the Command Line Interface (CLI) the registry Use the website to discover and download packages, create user profiles, and...:
npm install hapi --save
Launch the hapi-ness
Let’s take a closer look on what is happening here!
- First, we create a new
- 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
replyargument, 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
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
- server methods
Lifecycle of a request
hapi enables us a very granular control over incoming requests. The following happens to an incoming request:
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:
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:
server 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
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 Asynchrony, in software programming, refers to events that occur outside of the primary program flow and methods for dealing with them. External events such as signals or activities prompted by a program that occur at the same time as program execution without causing the program to block and wait for results are examples of this category. Asynchronous input/output is an... operation in your application’s bootstrapping phase.
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
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 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!
The features and mechanisms covered in this post hopefully get you interested in start using hapi.
Check out our guide: learn how to use generator functions with Hapi.