Creating a simple REST API with Node and Hapi in 10 min

In this tutorial we will create a simple REST API using the Node and Hapi framework. We will create a simple server with API which returns the books data and allow us to create, update and delete books.

Getting started

To follow this tutorial you should have the basic fundamentals of using command line.
All the following was done with the next requirements: Node v9.10.0, Hapi v17.8.1

Set up the project

Create a new folder and initialize a new project using the next command:

Then install Hapi framework with plugins, FS module to read/write data and Util module:

Now we can create the main file for our server. Let’s call it index.js and add the following code of very basic server:

Adding data

In our app we will use the JSON file as a main data store. Create a new books.json file in project folder and paste the next data:

We will make manipulations with this file on server, when the client makes the GET, POST, PUT, DELETE requests.

Getting list of books

Let’s create our first REST API endpoint to get the list of books. Add the following code in index.js file:

In the “handler” method we read the data from books.json file using the “readFile” function, then parse it using “JSON.parse” and send using the “.response” method. We need to parse this data, because the books variable it’s a string (JSON) and the server will send it as HTML text, but if “.response” method will take the pure javascript object, it sets all headers correctly. In this case client will understand that this is JSON.

When you access defined API route using GET http://localhost:3000/books you will get the list of all books frombooks.json file.

Creating a new book

To create a new book we need to define a POST /books route:

Inside handler, we setting the received data from client to book variable. Then reading the books data, setting a simple identifier and push a new book to books array. After all these actions, we rewriting the books.json and send an updated array of books to the client.

For example, if we make a POST request to /books endpoint with the next data:

Server will returns the next result:

Updating an existing book

In this section we will update an existing book by ID. We will send the book ID as a query params and new book data in request payload. Add the following code toindex.js:

The it’s ID from path. We using it to look an existing book in the books array. When we find it, we update it.

For example, if we make a PUT request to /books/1 endpoint with the next data:

Server will produce the next result:

Deleting an existing book

To remove an existing book from our books.json file, we will use the similar logic as we used in previous section. Let’s add the DELETE /books route to our server:

We filtering the existing array of books and exclude the book, which id we passed to our server. For example, if we make a DELETE request to /books/2 endpoint, we get the following result:

Full example


As you see, you can quickly create a basic REST API using the Hapi framework for 10 minutes. With this simple tutorial you can start to discover more complex topics like querying data from DB inside route handlers and sending to client, creating a separate folders for API routes, how to work with parameters or how to send files in your API.

Learning Hapi.js? Buy my Hapi.js Handbook🔥

If you liked this, please help spread the word by hitting the recommend button below. Thanks!