一、HapiJS介紹
HapiJS是一個開源的、基于Node.js的應(yīng)用框架,它適用于構(gòu)建應(yīng)用程序和服務(wù),其設(shè)計目標(biāo)是讓開發(fā)者把精力集中于開發(fā)可重用的應(yīng)用程序的業(yè)務(wù)邏輯,向開發(fā)者提供構(gòu)建應(yīng)用程序業(yè)務(wù)邏輯所需的基礎(chǔ)設(shè)施。HapiJS目前的最新版本為7.2.0版。
二、HapiJS安裝和項目配置
1、安裝Hapi庫
HapiJS的安裝很簡單,執(zhí)行如下命令:
$ 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、配置項目
1)創(chuàng)建一個名為myproject的新目錄
$ mkdir myproject
$ cd myproject
2)在目錄下運行初始化命令
$ npm init
此命令會生成package.json文件,它是項目的元數(shù)據(jù)。
接著執(zhí)行命令:
$ npm install --save hapi
它會安裝hapi庫到項目下,并把hapi的依賴關(guān)系寫入package.json。
此時,項目開發(fā)所需的一切都已經(jīng)準(zhǔn)備完畢。
三、開發(fā)實例
1、創(chuàng)建服務(wù)器
// server.js
var Hapi = require('hapi');
var server = new Hapi.Server(3000);
server.start(function(){
console.log('Server running at: ', server.info.uri);
});
首先,我們需要Hapi庫。
其次,我們創(chuàng)建一個新hapi服務(wù)器對象,并把要監(jiān)聽的端口號傳遞給服務(wù)器對象。
最后,服務(wù)器對象啟動,并輸出日志信息。
要說明一點,我們創(chuàng)建服務(wù)器對象時,可以提供主機名、IP地址,甚至是Unix的socket文件,或者是Windows系統(tǒng)綁定到服務(wù)器命名的管道。
2、啟動服務(wù)器
執(zhí)行命令:
$ node server.js
訪問http://127.0.0.1:3000/ ,瀏覽器顯示如下內(nèi)容:
{"statusCode":404,"error":"Not Found"}
很正常,因為服務(wù)器上本身就沒有任何內(nèi)容,下面把路由邏輯添加上。
3、路由邏輯
// 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);
});
再次啟動服務(wù)器:
$ node server.js
并訪問http://127.0.0.1:3000/ ,瀏覽器顯示如下內(nèi)容:
Hello, world!
訪問http://127.0.0.1:3000/張三 ,瀏覽器顯示如下內(nèi)容:
Hello, %E5%BC%A0%E4%B8%89!
可見,路由邏輯運行正常。
要注意:
method的參數(shù)可以是任意有效的HTTP方法,也可以是星號*(表示任意HTTP方法)。
path的參數(shù)定義了訪問路徑,路徑可以包含參數(shù)、可選參數(shù),甚至是通配符。
四、使用插件
在創(chuàng)建Web應(yīng)用時,通常我們都需要訪問日志。要為應(yīng)用程序添加基本的日志輸出,我們可以在服務(wù)器上加載good插件。
1、安裝good插件
$ 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、更新server.js的代碼
// 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);
});
});
運行server.js,控制臺輸出:
141102/161007.644, info, Server running at: http://localhost:3000
如果我們接著訪問:http://127.0.0.1:3000/liqiang
和http://127.0.0.1:3000/
控制臺會繼續(xù)輸出:
141102/161150.689, request, http://Thinker-LQ:3000: get /liqiang {} 200 (37ms)
141102/161155.812, request, http://Thinker-LQ:3000: get / {} 200 (4ms)