A brief introduction to HAPI

Hapi stands for HapiJS, which is a nodeJs-based development framework. Hapi is a simple, secure framework trusted by developers, according to the haPI homepage. Build powerful, scalable applications with minimal coding and out-of-the-box functionality — “Your code is your master.”

Hapi documents are available in five languages including Chinese and English. The current version is 18.4.

Hapi is widely used abroad and has a rich ecology. Robust documentation, canonical code style, and simple module and plug-in mechanisms make extending HAPI easier.

Hapi is not widely used in China. “Hidden in the mountains people do not know.” This article demonstrates the power and simplicity of HAPI by quickly creating a blog project API. This paper introduces the implementation of haPI project’s basic functions of adding, deleting, modifying and checking. Later, it will introduce the extended functions of HAPI project, such as automatic interface document generation, data verification, user authentication and file uploading, and finally complete a complete open source project example.

hapi-scaffold

Hapi-scaffold is an open source project on Github. Hapi-scaffold doesn’t need to write a single line of code to help you build a standard RESTful project with the MVC project structure and all the code required for the project. This is a powerful productivity tool for front end engineers.

Hapi, scaffold link: https://github.com/jeffsouza/hapi-scaffold

Create a project

Create a Posts project folder on your desktop and go to the project directory.

cd desktop
mkdir posts
cd posts
Copy the code

Initialization project:

npm int
Copy the code

The HApi-scaffold module is installed globally.

$ npm install hapi-scaffold -g
Copy the code

Initialize the project structure

Install the HAPI and Mongoose modules.

$ npm i @hapi/hapi mongoose
Copy the code

Initialize the HAPI project structure.

$ hapi-scaffold init
Copy the code

Hapi-scaffold automatically creates the following HAPI project structure in the project directory:

Connecting to the mongoDB Database

Entering the following command will create the database link file dadabase.js in the config folder.

$ hapi-scaffold generate db:mongo
Copy the code

Dadabase.js contains the following contents:

const mongoose = require('mongoose');
class Database {
    static connect() {
        mongoose.connect('URL', {useNewUrlParser: true});
    }
}
module.exports = Database;
Copy the code

Change the mongoDB connection URL to your own address, such as:

mongoose.connect('mongodb://localhost:27017/posts', {useNewUrlParser: true, useUnifiedTopology: true});
Copy the code

One key generates CRUD

Type the following command to create the posts model, controller, route, and other files.

$ hapi-scaffold scaffold post title:string:required content:string rating:number
Copy the code

Done! The screen will display:

Model created!
Service created!
Controller created!
Route created!
Route registered!
Copy the code

The add, delete, change and check API for posts based on Hapi is simply generated!

Let’s look at the directory structure of the project:

test

Enter the following code on the command line to run the project:

node .
Copy the code

The project runs successfully at the specified url:

Listening on http://localhost:8000
Copy the code

Use postman to add two pieces of data:

Select * from postman;

Modify the second data with Postman:

Delete the second data with postman:

Now the second blog post has been deleted:

Is it So easy,So hapi?