How to create a Hapi Server?

Today, I will share with you guys how to create the Hapi Server. First, you need to install the Hapi framework to your project. Please see my previous post about installing the Hapi.js framework.

Then, create the new server.js file under your project directory and copy the below codes to that file.

'use strict';

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

const init = async () => {

    const server = Hapi.server({
        port: 3000,
        host: 'localhost'
    });

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

process.on('unhandledRejection', (err) => {
    console.log(err);
    process.exit(1);
});

init();

After that, start the server using the following command.
$ npm start server.js

That’s it. I hope this will help. 🙂
Original Creator: Eran Hammer from WalmartLabs
Ref: https://hapi.dev/tutorials/?lang=en_US

Leave a Reply

Your email address will not be published.