Subscirbe to Kodhus news letter and get new tips and tutorials right in your inbox by entering your email below

How to write a backend API in nodeJS using hapi

Kodhus >
Like & share to be notified of the new tips & tutorials:

Nowadays professional websites and web interfaces that need to load data asynchronously build client side applications that run in the browser using javascript and fetch their data whether they are images or a user’s personal profile info from a backend API(Application programming interface). Today we are going to make a very simple API in nodeJS which is a technology based on javascript to build server side applications. Read more about it on their website. Since nodeJS is based on javascript and its’ fast, it has become very popular to develop APIs that could be consumed by frontend applications. The API will provide couple of endpoints that let us add a TODO item to our database, delete it, update it and also list all the TODOs that we have. For persisting this data we use mongodb database. Mongodb is a no-sql document oriented database meaning that we are not having tables with predefined columns and relational databases. rather we store the data in a dynamic fashion without any restriction of the schema using json format.

Ok, Let’s get started.


In order to write an API we could choose from a list of frameworks that are built for nodeJS which provide us for fast and reliable build of the APIs. When we are writing an API it’s very important to choose a framework that is light enough and fast. There are several of them. The most famous one is expressJS. We are not going to use it though because for our usecase which is just 4 simple calls to operate on todos, expressJS is an overkill since it’s heavy. We will use hapi. hapi is a rich framework on top of nodeJS for building applications and services. It’s quite lightweight and has a very good documentation and syntax.

Our goal

We want to have the following 4 endpoints that we can call to add, save, delete and retrieve them.

1. Retrieve saved TODOs

2. Add a TODO

3. Update a TODO

4. Delete a TODO

Alright! Let’s talk about these addresses a bit. You can see that http://localhost:8000 is basically our server address and port. For putting it in production or a real domain, it will look like this Other thing is that we have added a convention that these endpoints are API endpoints and also the API version is 1. We have presented this as http://localhost:8000/api/v1/todos. This basically means that our API is at version 1. Later if it needs improvement or changes, you need to change it to v2 so that you kind of track your API version as well and if the changes are drastic and you have some interfaces that still use v1 then you let them continue working.

Alright, Let’s now start some coding to bring the server up and running.

Initial setup

Go ahead and download nodeJS for the operating system you are using. I’m using mac. Install it. You also need to have mongodb installed on your computer as well. When you have it installed open a terminal window and type mongod which runs mongodb for us.

When you are done, create a folder for our project. Inside it run the following two commands to install hapi and mongoose modules.

npm is basically Nodejs’s package manager responsible for downloading and dealing with nodeJS packages that we want to use in our application.

We need to write some code for making a server and then after that we add some routes(addresses that we define to be called). We also use a library called Mongoose to be able to connect to our mongodb database.

It is pretty straightforward. We require hapi and mongoose, we setup a hapi server by creating it and then assign the settings for our server host and port. Host is localhost meaning that our current local address on our computer. Then we setup up mongoose by giving it the address of our mongodb database. Now we need to define the TODO model with the fields that we want it to have.

So, we first define our Schema and then create our model based on that schema. we use Todo object to interact with our todo database.

Now we need to create our routes. Let’s do it.


As you can see we need to set couple of things when defining our routes. First, we need to define an http method. You can read more about http methods in here. But for now, we will use GET for getting the TODOs, POST for adding a TODO, PUT for updating a TODO and DELETE for deleting a TODO.


This is the address that we will for our endpoints.


This is basically the function that does the magic for us. It accepts two parameters, request and rely. request contains the properties that are sent from the consumer such as query parameters and then reply is what we send back. Lets take the DELETE call as an example. What happens in the handler is that based on the path we can get the id of a todo and then using mongoose API we can first find that todo and then remove it using the remove and then returning back something in our case it is {status: doc} which is the todo that is deleted.

Final part is to actually run our server.

Save this file as index.js. Go to terminal in the project folder and run node index.js. Now go to browser and call the following address.

You will see that it returns an empty array since we have not added any todos. Now let’s add a todo to our database. We will use curl to call the /add endpoint passing the todo information that we want.

You can see that we define the content type to be application/json meaning that we are sending json data. We pass our todo text and then we add the endpoint http://localhost:8000/api/v1/add that we want this data to go to. Now if you go to the browser and ask for http://localhost:8000/api/v1/todos You will see the following:

You can see that our My first todo is added to our database. In the next tutorial we are going to create the frontend application using AngularJS 2 to interact with this API. This tutorial is intermediate to advanced so if you have any comments or questions please add it to the comment section. I will be glad to help you out setting up this and I will definitely make a video tutorial to give you a better understanding of the core concepts.