Restful CRUD API with hapi.js

Swarup A. Kharul
ACM VIT

--

Introduction

Hapi.js (shorthand for Http-API, pronounced happy and also known as hapi) is an open-source Node.js framework used to build powerful and scalable web applications. Hapi is commonly used to build Application Programming Interface (API) servers, HTTP-proxy applications, and websites. Hapi.js was created by a team at Walmart Labs to handle their traffic for events like Black Friday, which is by far one of the busiest days for online shopping on the U.S. calendar. Hapi was originally built using the express framework before facing challenges that drove Walmart to make hapi, its own stand-alone framework.

What is CRUD?

CRUD is an acronym for Create, Read, Update and Delete. It is a set of operations we get servers to execute (POST, GET, PUT and DELETE requests respectively). This is what each operation does:

  • Create (POST) — Make something
  • Read (GET)- Get something
  • Update (PUT) — Change/Edit something
  • Delete (DELETE)- Remove something

POST, GET, PUT, and DELETE requests let us construct Rest APIs.

Great! Let’s move on.

Prerequisites

Before building your API make sure you have Node.js and MongoDB installed and setup on your machine before starting this tutorial. The complete code can be found here.

Getting Started

The first step in any new project is setting up the directory. So, fire up your terminal and create a new directory for the application.

mkdir CRUD_App
cd CRUD_App

Initialize the application with a package.json file

npm init

Install Hapi

npm install @hapi/hapi

Creating a simple Server

A very basic hapi server looks like the following and displays “Hello World!” in your browser.

Save the above as index.js and start the server with the command node index.js. Now visit http://localhost:3000 in your browser, and voila!! You have setup your own server.

Database connection using Mongoose ODM

Install Mongoose ODM

npm install mongoose

Add this code in the beginning to connect to mongodb using the mongoose ODM.

Great! Now that we have learnt the basics it’s time to start with the CRUD operations.

We will be making a simple CRUD Restful API for a Notes App.

Creating the schema

The schema defines the structure of our database. Schema helps to organize the documents in the collection. Since we are creating a Note, our schema should contain the following fields

  • title
  • important
  • description

The schema for the data is created using the mongoose.model method. The type of data should be specified.

Create a Note

To create a Note we send a post request which contains the data. request.payloadcontains the data that we pass from the frontend. We create a new instance of the Note model we created and saves it to DB using the collection.save method. Then the response is returned.

Get all Notes with filter

To get a list of all the notes we send a get request. We can also pass parameters to get a filtered list. For example '/api/notes?important=true’ will give a list of all the important notes.

Update a Note

For updating the Note, we will use the object id of the Note. We pass the object id of the Note as a parameter of the request.

Delete a Note

Deleting the Note is also done using the object id of the Note. For deleting the Note, we pass the object id of the Note as a parameter of the request.

Final Thoughts:

Now that your API is all ready to go you can test it out yourself using Postman with the URL http://localhost:3000/api. The complete code for this blog can be found here.

If it was interesting or helpful to you, please do press the 👏 clap button and help others find this story too.

Did you know you could give up to 50 claps?

--

--

Swarup A. Kharul
ACM VIT
Writer for

Engineering Student. Full Stack Developer and ML Enthusiast