1. Introduction to HapiJS
HapiJS is an open source, Node.js-based application framework, which is suitable for building applications and services. Its design goal is to allow developers to focus on developing business logic for reusable applications and provide developers with the infrastructure needed to build application business logic. The latest version of HapiJS is currently version 7.2.0.
2. HapiJS installation and project configuration
1. Install the Hapi library
The installation of HapiJS is very simple, execute the following command:
The code copy is as follows:
$ 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])
2. Configure the project
1) Create a new directory called myproject
The code copy is as follows:
$ mkdir myproject
$ cd myproject
2) Run the initialization command in the directory
The code copy is as follows:
$ npm init
This command generates a package.json file, which is the metadata of the project.
Then execute the command:
The code copy is as follows:
$ npm install --save hapi
It will install the hapi library into the project and write the hapi dependencies to package.json.
At this point, everything required for project development is ready.
III. Development examples
1. Create a server
The code copy is 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 the Hapi library.
Secondly, we create a new hapi server object and pass the port number to the server object.
Finally, the server object is started and the log information is output.
To be clear, when we create a server object, we can provide the host name, IP address, or even a Unix socket file, or a pipeline bound to the server name by the Windows system.
2. Start the server
Execute the command:
The code copy is as follows:
$ node server.js
Visit http://127.0.0.1:3000/, and the browser displays the following content:
The code copy is as follows:
{"statusCode":404,"error":"Not Found"}
It's normal, because there is nothing on the server itself, so add the routing logic below.
3. Routing logic
The code copy is 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:
The code copy is as follows:
$ node server.js
And visit http://127.0.0.1:3000/, and the browser displays the following content:
Hello, world!
Visit http://127.0.0.1:3000/Zhang San, and the browser displays the following content:
Hello, %E5%BC%A0%E4%B8%89!
It can be seen that the routing logic is running normally.
Note:
The parameter of the method can be any valid HTTP method or an asterisk* (represents any HTTP method).
The path parameter defines the access path, which can contain parameters, optional parameters, and even wildcards.
4. Use 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 the good plugin on the server.
1. Install good plug-in
The code copy is as follows:
$ sudo npm install --save good
[email protected] node_modules/good
├── [email protected] ([email protected])
└── [email protected] ([email protected], [email protected])
2. Update server.js code
The code copy is 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:
The code copy is as follows:
141102/161007.644, info, Server running at: http://localhost:3000
If we continue to visit: http://127.0.0.1:3000/liqiang
and http://127.0.0.1:3000/
The console will continue to output:
The code copy is as follows:
141102/161150.689, request, http://Thinker-LQ:3000: get /liqiang {} 200 (37ms)
141102/161155.812, request, http://Thinker-LQ:3000: get / {} 200 (4ms)