forked from DefinitelyTyped/DefinitelyTyped
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhapi-tests-8.2.0.ts
99 lines (81 loc) · 2.05 KB
/
hapi-tests-8.2.0.ts
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/// <reference path="hapi.d.ts" />
import Hapi = require("hapi");
// Create a server with a host and port
var server = new Hapi.Server();
server.connection({
host: "localhost",
port: 8000,
});
// Add plugins
var plugin: any = {
register: function (plugin: Object, options: Object, next: Function) {
next();
}
};
plugin.register.attributes = {
name: "test",
version: "1.0.0"
};
// optional options parameter
server.register({}, function (err) {});
// optional options.routes.vhost parameter
server.register({}, { select: 'api', routes: { prefix: '/prefix' } }, function (err) {});
//server.pack.register(plugin, (err: Object) => {
// if (err) { throw err; }
//});
//server.pack.register([plugin], (err: Object) => {
// if (err) { throw err; }
//});
// Add server method
var add = function (a: number, b: number, next: (err: any, result?: any, ttl?: number) => void) {
next(null, a + b);
};
server.method("sum", add);//, { cache: { expiresIn: 2000 } });
server.methods["sum"](4, 5, (err: any, result: any) => {
console.log(result);
});
var addArray = function (array: Array<number>, next: (err: any, result?: any, ttl?: number) => void) {
var sum: number = 0;
array.forEach((item: number) => {
sum += item;
});
next(null, sum);
};
server.method("sumObj", addArray, {
//cache: { expiresIn: 2000 },
generateKey: (array: Array<number>) => {
return array.join(',');
}
});
server.methods["sumObj"]([5, 6], (err: any, result: any) => {
console.log(result);
});
// Add the route
server.route({
method: 'GET',
path: '/hello',
handler: function (request: Hapi.Request, reply: Function) {
reply('hello world');
}
});
server.route([{
method: 'GET',
path: '/hello2',
handler: function (request: Hapi.Request, reply: Function) {
reply('hello world2');
}
}]);
// config.validate parameters should be optional
server.route([{
method: 'GET',
path: '/hello2',
handler: function(request: Hapi.Request, reply: Function) {
reply('hello world2');
},
config: {
validate: {
}
}
}]);
// Start the server
server.start();