Introduction to Node. js open-source application framework HapiJS, node. jshapijs
1. Introduction to HapiJS
HapiJS is an open-source, Node-based. the js application framework is suitable for building applications and services. Its design goal is to allow developers to focus on developing reusable application business logic, provide developers with the infrastructure required to build application business logic. The latest version of HapiJS is 7.2.0.
Ii. HapiJS installation and project configuration
1. Install the Hapi Library
The installation of HapiJS is simple. Execute the following command:
Copy codeThe Code is 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. configuration items
1) create a new directory named myproject
Copy codeThe Code is as follows:
$ Mkdir myproject
$ Cd myproject
2) run the initialization command in the directory
Copy codeThe Code is as follows:
$ Npm init
This command generates the package. json file, which is the metadata of the project.
Run the following command:
Copy codeThe Code is as follows:
$ Npm install -- save hapi
It installs the hapi library under the project and writes the hapi dependency to package. json.
At this point, everything required for project development is ready.
Iii. Development Instance
1. Create a server
Copy codeThe Code 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.
Next, we create a new hapi Server Object and pass the port number to be listened on to the server object.
Finally, the server object is started and the log information is output.
Note: When creating a server object, you can provide the host name, IP address, Unix socket file, or the pipe bound to the server name in Windows.
2. Start the server
Run the following command:
Copy codeThe Code is as follows:
$ Node server. js
Access http: // 127.0.0.1: 3000/. The browser displays the following content:
Copy codeThe Code is as follows:
{& Quot; statusCode & quot;: 404, & quot; error & quot;: & quot; Not Found & quot "}
Normally, because there is no content on the server itself, the routing logic is added below.
3. Routing Logic
Copy codeThe Code 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:
Copy codeThe Code is as follows:
$ Node server. js
Visit http: // 127.0.0.1: 3000/. The browser displays the following content:
Hello, world!
Visit http: // 127.0.0.1: 3000/Zhang San. the browser displays the following content:
Hello, % E5 % BC % A0 % E4 % B8 % 89!
The routing logic runs normally.
Note:
The method parameter can be any valid HTTP method or asterisk ).
The path parameter defines the access path, which can contain parameters, optional parameters, and even wildcards.
Iv. Use plug-ins
When creating a Web application, we usually need access logs. To add basic log output to an application, we can load the good plug-in on the server.
1. Install the good plug-in
Copy codeThe Code is 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 codeThe Code 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 and the console outputs:
Copy codeThe Code is as follows:
141102/161007. 644, info, Server running at: http: // localhost: 3000
If we continue to access: http: // 127.0.0.1: 3000/liqiang
And http: // 127.0.0.1: 3000/
The console will continue to output:
Copy codeThe Code is as follows:
141102/161150. 689, request, http: // Thinker-SCSI: 3000: get/liqiang {} 200 (37 ms)
141102/161155. 812, request, http: // Thinker-SCSI: 3000: get/{} 200 (4 ms)