-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
63 lines (53 loc) · 1.41 KB
/
server.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
'use strict';
const Hapi = require('hapi');
const server = Hapi.server({
host: 'localhost',
port: 3000
});
server.route({
method: 'GET',
path: '/',
/* Here to correct the statusCode 400 because of Invalid Cookie Value */
config: {
state: {
parse: false, // parse and store in request.state
failAction: 'ignore' // may also be 'ignore' or 'log'
}
},
/* h is the response toolkit, an object with several methods
used to respond to the request */
handler: (request, h) => {
return 'Hello, world!';
},
});
server.route({
method: 'GET',
path: '/{name}',
config: {
state: {
parse: false, // parse and store in request.state
failAction: 'ignore' // may also be 'ignore' or 'log'
}
},
handler: (request, h) => {
/* request.log(['a', 'name'], "Request name"); or */
request.logger.info('In handler %s', request.path);
return `Hello, ${encodeURIComponent(request.params.name)}!`;
}
});
const init = async () => {
await server.register({
plugin: require('hapi-pino'),
options: {
prettyPrint: false,
logEvents: ['response']
}
});
await server.start();
console.log(`Server running at: ${server.info.uri}`);
};
process.on('unhandledRejection', (err) => {
console.log(err);
process.exit(1);
});
init();