HApi课堂实验手册(五)

上一个实验 中,我们将演示了 Route 多样的返回类型,本次实验将聚焦在 Server 的生命周期方法上。

示例程序

新建一个名为: RequestCycle.js 的文件。在文件中填入以下代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
const Hapi = require('hapi');

const server = new Hapi.Server({
host: 'localhost',
port: 8000
});

server.route({
method: 'GET',
path: '/',
handler: function (request, h) {

return 'Hello World\n';
}
});

server.ext([{
type: 'onRequest',
method: async (request, h) => {
console.log('[onRequest]...')
return h.continue
}
},{
type: 'onPostHandler',
method: async (request, h) => {
console.log('[onPostHandler]...data: %s', request.response.source)
return h.continue
}
}
])

const start = async function() {

try {
await server.start();

console.log('Server running at:', server.info.uri);

} catch(err) {
console.log(err);
process.exit(1);
}
}

start();

本文标题:HApi课堂实验手册(五)

文章作者:Morning Star

发布时间:2019年11月09日 - 12:11

最后更新:2021年04月16日 - 15:04

原始链接:https://www.mls-tech.info/node/hapi-practise-manual-05/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。