-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex-helloworld.js
66 lines (53 loc) · 1.46 KB
/
index-helloworld.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
'use strict'
// Rendering JsRender templates using Hapi https://hapi.dev/tutorials/views/
// Use this file as start file, after installing Hapi: npm install --save @hapi/hapi
var Hapi = require('@hapi/hapi');
var Path = require('path');
var start = async () => {
var server = Hapi.server({
port: 3000,
host: 'localhost',
routes: {
files: {
relativeTo: Path.join(__dirname, 'public')
}
}
});
await server.register(require('@hapi/inert'));
await server.register(require('@hapi/vision'));
// Set public directory as root for static files
server.route({
method: 'GET',
path: '/{param*}',
handler: {
directory: {
path: '.',
redirectToSlash: true,
index: true
}
}
});
// Load JsRender library
var jsrender = require('jsrender');
////////////////////////////////////////////////////////////////
// Set JsRender as the template engine for Hapi
server.views({
engines: { html: jsrender },
relativeTo: __dirname,
path: 'templates'
});
var context = { hello: "world" };
////////////////////////////////////////////////////////////////
// Render layout.html template as 'home page' using Hapi
server.route({
method: 'GET',
path: '/',
handler: function (request, h) {
// Render helloworld.html template
return h.view('helloworld', context);
}
});
await server.start();
console.log('Server running at:', server.info.uri);
};
start();