How to build APIs using hapi.js in Node.js

By reading this tutorial you can build RESTful APIs using Node, hapi.js, and Sequelize ORM. We gonna use the different HTTP methods during the REST API development, quick introduction to each method and route details along with the description of each endpoint you can find in the below table.

# Route Method Description
1 /todos GET Get all todos data
2 /todo/{id} GET Get a single todo data
3 /create POST Create new todo record in database
4 /update/{id} PUT Update an todo record
5 /delete/{id} DELETE Delete an todo record
6 /todos/search/{term} GET Search for todo, ex: php

Each method as a purpose isn’t it?. Let me give an introduction to the app which you are going to build is a TODO application. You can add a todo to list, update, delete todos and you can even search for todos.

Install sequelize-cli

Following npm command installs sequelize-cli globally. It means you can access from anywhere via terminal.

Project setup

Lets create a folder for the project, for example todo-app. And change the directory to newly created directory(Ex: todo-app) and issue npm init --yes command to generate package.json in your project folder. Once it is done, then install the project npm depencies called hapi, mysql2, sequelize with follging command npm install hapi mysql2 sequelize

Command which we have exectired in the preceeding text

And your final package.json file should look something like below shown

Initializes sequelize

let use the Sequelize CLI to generate migrations, seeders, config and models directories and config file.

After executing sequelize init command, your folder structure should be similar to below shown

hapi js rest tutorial | arjunphp.com

If you are windows user correct the config file path in models/index.js by changing
var config = require(__dirname + '/..\config\config.json')[env]; to var config = require(__dirname + '/../config/config.json')[env];

Database Configurations

Now config.json file which is located at config\config.json and updates your database details

Create Models and Migrations

Again we gonna use sequelize cli command to generate model and migrations files.

Above command generates a todo.js file in the PROJECT_ROOT/models folder as well as a -create-todo.js migration file in the PROJECT_ROOT/migrations folder. will be the date the model was generated.

Create Models and Migrations ArjunPHP

PROJECT_ROOT/models/todo.js

Here is the generated model code, you can add or remove columns to it, make sure to update migration file as for your changes on this model.

PROJECT_ROOT/migrations/20180928035740-create-todo.js

Here is the generated migration code, you can add or remove columns to it, make sure to update model file as for your changes on this migration file.

Run Migrations

You can create database tables by running migrations with:

That’s it, now you can check your database, you should able to see new tables in your database.

Implementing the API calls with HapiJs

Create a file called server.js in the project root folder with below code:

Now create a folder called routes in the root of the project along with index.js file and place following code inside it.

Now create a file called todos.js as imported in routes/index.js file and copy following code to it.

That’s it, hapi.js Rest server is ready, you can start the server with node server.js and you can access it at http://localhost:3000 as metioned in server.js file.

I hope you like this Post, Please feel free to comment below, your suggestion and problems if you face - we are here to solve your problems.

Arjun
I am Arjun from Hyderabad (India). I have been working as a software engineer from last 7+ years, and its my passion to learn new things and implement them as a practice. Aside from work, I likes gardening and spending time with pets.
DMCA.com Protection Status