-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
104 lines (91 loc) · 2.18 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
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
100
101
102
103
104
'use strict';
const Blankie = require('blankie');
const Hapi = require('@hapi/hapi');
const Inert = require('@hapi/inert');
const Path = require('path');
const Pino = require('hapi-pino');
const Pug = require('pug');
const Scooter = require('@hapi/scooter');
const Vision = require('@hapi/vision');
const constants = require('./js/constants');
const exitValue = 1;
const server = new Hapi.Server({
host: 'localhost',
port: 13000,
routes: {
files: {
relativeTo: Path.join(__dirname, '/')
}
}
});
const begin = async() => {
await server.register([
{
plugin: Scooter
},
{
plugin: Blankie,
options: {
defaultSrc: 'self',
imgSrc: ['self', 'data:'],
objectSrc: 'none'
}
},
{
plugin: Inert
},
// ,
{
plugin: Pino
},
{
plugin: Vision
}], {});
// err => {
// if (err) {
// console.error(err);
// /* eslint-disable no-process-exit */
//
// process.exit(exitValue);
// /* eslint-enable */
// }
server.views({
engines: {
pug: Pug
},
path: `${__dirname}/views`,
compileOptions: {
pretty: true
},
isCached: false
});
// default file handler via Inert
server.route({
method: 'GET',
path: '/{filename*}',
handler: {
file: (request) => {
return request.params.filename;
}
}
});
server.route({
method: 'GET',
path: '/',
handler: (request, reply) => {
return reply.view('index', {
constants: constants
});
}
});
// });
await server.start();
server.logger().info(` 💻 Server running at ${server.info.uri}`);
};
server.ext('onPreResponse', async(request, reply) => {
if (!request.response.isBoom) {
return reply.continue;
}
return reply.view('error', request.response).code(request.response.output.statusCode);
});
begin();