Getting Started with Server-Side Development with Hapi.js

Spread the love

Hapi.js is a small Node framework for developing back end web apps.

In this article, we’ll look at how to create back end apps with Hapi.js.

Getting Started

We can start by creating a project folder, go into it, and run:

npm install @hapi/hapi

to install the package.

Creating a Server

Once we installed the package, we can create a simple back end app by writing:

const Hapi = require('@hapi/hapi');

const init = async () => {
  const server = Hapi.server({
    port: 3000,
    host: ''

    method: 'GET',
    path: '/',
    handler: (request, h) => {
      return 'Hello World!';

await server.start();
  console.log('Server running on %s',;

process.on('unhandledRejection', (err) => {


We require the hapi package.

Then we run the Hapi.server method to create the server.

host is set to '' to listen for request from all IP addresses.

And then to create a route, we call the server.route method.

The method is the request method.

handler is the request handler function.

Then we call server.start to start the server.

Next, we add the unhandledRejection error handler to exit the app gracefully when the app crashes.


We can add authentication to our app with a few lines of code.

For instance, we can write:

const Bcrypt = require('bcrypt');
const Hapi = require('@hapi/hapi');

const users = {
  john: {
    username: 'john',
    password: '$2a$10$iqJSHD.BGr0E2IxQwYgJmeP3NvhPrXAeLSaGCj6IR/XU5QtjVu5Tm',
    name: 'John Doe',
    id: '1'

const validate = async (request, username, password) => {
  const user = users[username];
  if (!user) {
      return { credentials: null, isValid: false };

const isValid = await, user.password);
  const credentials = { id:, name: };
  return { isValid, credentials };

const start = async () => {
  const server = Hapi.server({ port: 4000 });
  await server.register(require('@hapi/basic'));
  server.auth.strategy('simple', 'basic', { validate });
    method: 'GET',
    path: '/',
    options: {
        auth: 'simple'
    handler(request, h) {
      return 'welcome';
  await server.start();
  console.log('server running at: ' +;


to add simple authentication with the bcrypt and the hapi-basic modules.

The users object has the user data.

The validate function gets the request data from the request parameter.

We get the user by the username .

Then we call to compare the password we entered with the hash we stored in the users object’s password property.

Then we have:

server.auth.strategy('simple', 'basic', { validate });

to add the basic authentication strategy.

And we define our route with the server.route .

The options.auth property is set to the name of the authentication strategy we added.

Now when we go to the / route, we see the login dialog box.

And when we enter john for username and secret for password, we see the welcome response.


We can create a simple app with authentication with Hapi.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *