Getting Started

Overview

This tutorial will show how to set up a basic hapi server that displays "Hello World!" in your browser.

Installing hapi

Create a new directory myproject, and from there:

  • Run: cd myproject, this goes into the created project folder.

  • Run: npm init and follow the prompts. This will generate a package.json file for you.

  • Run: npm install @hapi/hapi, this will install the latest version of hapi as a dependency in your package.json.

Creating a Server

A very basic hapi server looks like the following:

  1. 'use strict';
  2.  
  3. const Hapi = require('@hapi/hapi');
  4.  
  5. const init = async () => {
  6.  
  7. const server = Hapi.server({
  8. port: 3000,
  9. host: 'localhost'
  10. });
  11.  
  12. await server.start();
  13. console.log('Server running on %s', server.info.uri);
  14. };
  15.  
  16. process.on('unhandledRejection', (err) => {
  17.  
  18. console.log(err);
  19. process.exit(1);
  20. });
  21.  
  22. init();

First, you require hapi. Then you initialize a new Hapi.server() with connection details containing a port number to listen on and the host information. After that you start the server and log that it's running.

When creating a server, you can provide a hostname, IP address, a Unix socket file, or Windows named pipe to bind the server to. For more details, see the API reference.

The host property set to localhost is likely the safest choice. In a docker container, however, the localhost may not be accessible outside of the container and using host: '0.0.0.0' may be needed.

Adding Routes

After you get the server up and running, its time to add a route that will display "Hello World!" in your browser.

  1. 'use strict';
  2.  
  3. const Hapi = require('@hapi/hapi');
  4.  
  5. const init = async () => {
  6.  
  7. const server = Hapi.server({
  8. port: 3000,
  9. host: 'localhost'
  10. });
  11.  
  12. server.route({
  13. method: 'GET',
  14. path:'/',
  15. handler: (request, h) => {
  16.  
  17. return 'Hello World!';
  18. }
  19. });
  20.  
  21. await server.start();
  22. console.log('Server running on %s', server.info.uri);
  23. };
  24.  
  25. process.on('unhandledRejection', (err) => {
  26.  
  27. console.log(err);
  28. process.exit(1);
  29. });
  30.  
  31. init();

Save the above as index.js and start the server with the command node index.js. Now you'll find that if you visit http://localhost:3000 in your browser, you'll see the text 'Hello, World!'.

The method property can be any valid HTTP method, array of HTTP methods, or an asterisk to allow any method.

The path property defines the path including parameters. It can contain optional parameters, numbered parameters, and even wildcards. For more details, see the routing tutorial.

The handler function performs the main business logic of the route and sets the response. The handler must return a value, a promise, or throw an error.

Everything Else

hapi has many, many other capabilities and only a select few are documented in tutorials here. This tutorial was intentionally minimal, we highly recommend you to check out the Plugins tutorial. It will give some more knowledge to better organize your hapi project. We also have other tutorials of interest, please use the list to your right to check them out. Everything else is documented in the API reference and, as always, feel free to ask questions on github or just visit us on slack.