Node.js Open Source application Framework Hapijs Introduction _node.js

Source: Internet
Author: User
Tags json node server

First, Hapijs Introduction

Hapijs is an open source, node.js application framework for building applications and services that is designed to allow developers to focus on developing business logic for reusable applications, providing developers with the infrastructure needed to build application business logic. The latest version of Hapijs is the 7.2.0 edition.

Second, Hapijs installation and project configuration

1. Install Hapi Library
the installation of Hapijs is simple, and the following command is executed:

Copy Code code as follows:

$ sudo npm install hapi-g
Hapi@7.2.0/usr/local/lib/node_modules/hapi
├──cryptiles@2.0.4
├──heavy@1.0.0
├──topo@1.0.2
├──accept@1.0.0
├──items@1.1.0
├──kilt@1.1.1
├──catbox-memory@1.1.0
├──boom@2.5.1
├──qs@2.2.4
├──call@1.0.0
├──statehood@1.2.0
├──h2o2@2.0.1
├──iron@2.1.2
├──shot@1.3.5
├──glue@1.0.0
├──wreck@5.0.1
├──hoek@2.8.0
├──catbox@4.0.3
├──vision@1.1.0
├──mimos@1.0.0 (mime-db@1.1.1)
├──rejoice@1.0.0 (bossy@1.0.2)
├──inert@1.1.0 (lru-cache@2.5.0)
├──joi@4.7.0 (isemail@1.1.1)
└──subtext@1.0.1 (content@1.0.1, pez@1.0.0)

2, configure the project

1 Create a new directory named MyProject

Copy Code code as follows:

$ mkdir MyProject
$ CD MyProject



2 run the initialization command in the directory


Copy Code code as follows:

$ NPM Init



This command generates the Package.json file, which is the metadata for the project.


Then execute the command:


Copy Code code as follows:

$ NPM Install--save Hapi



It installs the HAPI library under the project and writes the Hapi dependencies to Package.json.

At this point, everything you need to develop the project is ready.

Iii. Examples of development

1. Create Server

Copy Code code as follows:

Server.js
var Hapi = require (' Hapi ');
var server = new Hapi.server (3000);

Server.start (function () {
Console.log (' Server running at: ', Server.info.uri);
});




First, we need to hapi the library.


Second, we create a new Hapi server object and pass the port number we want to listen to the server object.


Finally, the server object is started and the log information is output.


To illustrate, when we create a server object, we can provide a host name, an IP address, even a UNIX socket file, or a Windows system bound to a server-named pipe.

2, start the server
To execute a command:

Copy Code code as follows:

$ node Server.js



To access Http://127.0.0.1:3000/, the browser displays the following:


Copy Code code as follows:

{"StatusCode": 404, "Error": "Not Found"}

Very normal, because there is no content on the server itself, the following route logic is added.

3. Routing logic

Copy Code code as follows:

Server.js
var Hapi = require (' Hapi ');
var server = new Hapi.server (3000);

Server.route ({
Method: ' Get ',
Path: '/',
Handler:function (Request, Reply) {
Reply (' Hello, world! ');
}
});

Server.route ({
Method: ' Get ',
Path: '/{name} ',
Handler:function (Request, Reply) {
Reply (' Hello, ' + encodeuricomponent (request.params.name) + "!");
}
});

Server.start (function () {
Console.log (' Server running at: ', Server.info.uri);
});

Start the server again:

Copy Code code as follows:

$ node Server.js



and access to Http://127.0.0.1:3000/, the browser displays the following:


Hello, world!.


To access Http://127.0.0.1:3000/John, the browser displays the following:


Hello,%e5%bc%a0%e4%b8%89!.

Visible, the routing logic is working properly.

To note:
The parameter of method can be any valid HTTP methods, or it can be an asterisk * (representing any HTTP method).
The path parameter defines an access path that can contain parameters, optional parameters, or even wildcard characters.

Iv. Use of Plug-ins

When creating a Web application, we usually need to access the log. To add basic log output to the application, we can load good plug-ins on the server.

1. Install Good plugin

Copy Code code as follows:

$ sudo npm install--save good
good@3.1.1 Node_modules/good
├──json-stringify-safe@5.0.0
├──good-reporter@2.0.0
├──async@0.9.0
├──hoek@2.8.1
├──moment@2.8.3
├──good-file@2.0.0 (items@1.1.0)
└──joi@4.7.0 (topo@1.0.2, isemail@1.1.1)



2, update the Server.js code


Copy Code code as follows:

Server.js
var Hapi = require (' Hapi ');
var good = require (' good ');

var server = new Hapi.server (3000);

Server.route ({
Method: ' Get ',
Path: '/',
Handler:function (Request, Reply) {
Reply (' Hello, world! ');
}
});

Server.route ({
Method: ' Get ',
Path: '/{name} ',
Handler:function (Request, Reply) {
Reply (' Hello, ' + encodeuricomponent (request.params.name) + "!");
}
});

Server.pack.register (Good, function (err) {
if (err) {
Something bad happened loading the plugin
throw err;
}
Server.start (function () {
Server.log (' info ', ' Server running at: ' + Server.info.uri);
});
});




Run Server.js, console output:


Copy Code code as follows:

141102/161007.644, info, Server running at:http://localhost:3000



If we proceed to visit: Http://127.0.0.1:3000/liqiang


and Http://127.0.0.1:3000/


The console will continue to output:


Copy Code code as follows:

141102/161150.689, request, Http://Thinker-LQ:3000:get/liqiang {} (37MS)
141102/161155.812, request, Http://Thinker-LQ:3000:get/{} (4MS)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.