In my previous post I mentioned that Node.js became synonymous with Express.js, and that there are a few other, more modern options out there that might be worth considering when starting a Node.js project from scratch.

In this post we will create a quick starter with another awesome framework called Hapi. Here is some context about the framework:

Originally developed to handle Walmart’s Black Friday scale, hapi continues to be the proven choice for enterprise-grade backend needs.

I hope this was enough insight to peak your interest 🤓. Let's get started!

Project setup

TL;DR you can find the repository with the code here.

Just like before, we're going to need a project directory, with two directories inside it. One we can call src (this is where we'll create the Typescript files) and the other one  build (this is where Typescript will transpile into).

Next step is to make sure we have Typescript installed, so we're just going to install it globally.

npm i -g typescript

We're also going to need a tsconfig.json file, that is responsible for configuring the compiler. Create it in your project directory and use the values below.

{
  "compilerOptions": {
    "outDir": "./build",
    "lib": [ "es5", "es6" ],
    "module": "commonjs",
    "target": "es6",
    "sourceMap": true
  },
  "exclude": [
    "node_modules"
  ]
}

Alright, now onto the Hapi project, let's type a few commands into our terminal (make sure you're in your project directory)

We're going to initiate a new Node.js project typing

npm init -y

And let's install some packages related to Hapi

npm i -S hapi

We're also adding type definitions for node and hapi

npm i -D @types/node @types/hapi

Now onto the coding part. 🙌

Create an Index.ts file in the src directory, and paste the code below into it.

import * as Hapi from "hapi";


const host = "localhost";
const port = 3000;


/** App */
const server: Hapi.Server = new Hapi.Server( { host, port } );



/** Routes */

server.route({
    method: "GET",
    path: '/',
    handler: (request: Hapi.Request, h: Hapi.ResponseToolkit) => "This is the GET route."
});



server.route({
    method: "POST",
    path: '/',
    handler: (request: Hapi.Request, h: Hapi.ResponseToolkit) => {
        return {
            message: "This is the POST route, the data you sent is attached to this response",
            data: request.payload
        }
    }
});



async function start() {
    try {
        await server.start();
    } catch (err) {
        console.log( err );
        process.exit( 1 );
    }
    console.log( `Server running @ ${ server.info.uri }` );
}

start();

You'll find a basic example of a GET route and a POST route in there. The POST route will return the data being sent to the server.

Now, let's add this snippet, instead of the scripts section in our package.json file and we should be good to go ✔️

"scripts": {
    "start": "tsc && node ./build/Index.js"
 },

You're all set. You can run npm start in your terminal and your server will be live on port 3000. 🎉🎉🎉