Hapijs Development Handbook

Source: Internet
Author: User
Tags node server

Hapijs Development HandbookChszs, reprint need to indicate. Blog home:Http://blog.csdn.net/chszs

I. Introduction of Hapijs

Hapijs is an open source, node. JS-based application framework for building applications and services designed to allow developers to focus on the business logic of developing reusable applications and provide developers with the infrastructure they need to build application business logic. The latest version of Hapijs is 7.2. version 0.

second, Hapijs installation and project configuration

1. Install Hapi Library
The installation of Hapijs is simple and executes the following command:
$ sudo npm install hapi-g
[Email protected]/usr/local/lib/node_modules/hapi
├──[email protected]
├──[email protected]
├──[email protected]
├──[email protected]
├──[email protected]
├──[email protected]
├──[email protected]
├──[email protected]
├──[email protected]
├──[email protected]
├──[email protected]
├──[email protected]
├──[email protected]
├──[email protected]
├──[email protected]
├──[email protected]
├──[email protected]
├──[email protected]
├──[email protected]
├──[email protected] ([email protected])
├──[email protected] ([email protected])
├──[email protected] ([email protected])
├──[email protected] ([email protected])
└──[email protected] ([email protected], [email protected])

2. Configuration Items

1) Create a new directory named MyProject
$ mkdir MyProject
$ CD MyProject

2) Run the init command in the directory
$ NPM Init
This command generates the Package.json file, which is the metadata for the project.
Then execute the command:
$ NPM Install--save Hapi
It installs the HAPI library into the project and writes the Hapi dependency to Package.json.

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

Iii. Examples of development

1. Create a server
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 the HAPI library.
Second, we create a new Hapi server object and pass the port number to be monitored to the server object.
Finally, the server object starts and outputs the log information.
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 pipe to a server named.

2. Start the server
Execute command:
$ node Server.js
To access Http://127.0.0.1:3000/, the browser displays the following content:
{"StatusCode": 404, "Error": "Not Found"}

is normal, because there is no content on the server itself, add the routing logic below.

3. Routing logic

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:
$ node Server.js
and access Http://127.0.0.1:3000/, the browser displays the following:
Hello, world!.
To access Http://127.0.0.1:3000/Zhang San, the browser displays the following content:
Hello,%e5%bc%a0%e4%b8%89!.

As you can see, the routing logic runs fine.
Be aware that:
The parameter of method can be any valid HTTP method, or it can be an asterisk * (means any HTTP method).
The path parameter defines the access path, which can contain parameters, optional parameters, and even wildcards.

Iv. use of plugins

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

1, install good plug-in
$ sudo npm install--save good
[Email protected] Node_modules/good
├──[email protected]
├──[email protected]
├──[email protected]
├──[email protected]
├──[email protected]
├──[email protected] ([email protected])
└──[email protected] ([email protected], [email protected])

2, update the Server.js code
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:
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:
141102/161150.689, request, Http://Thinker-LQ:3000:get/liqiang {} (37MS)
141102/161155.812, request, Http://Thinker-LQ:3000:get/{} (4MS)


Hapijs Development Handbook

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.