-
Notifications
You must be signed in to change notification settings - Fork 7
/
index-hapi-browserify.js
118 lines (100 loc) · 3.65 KB
/
index-hapi-browserify.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
'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 Fs = require('fs');
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'
});
////////////////////////////////////////////////////////////////
// Render layout-movies-browserify.html template as 'home' page using Hapi
server.route({
method: 'GET',
path: '/',
handler: function (request, h) {
return h.view('layout-movies-browserify', appData);
}
});
////////////////////////////////////////////////////////////////
// Render layout-hello-browserify.html template as 'hello/world' page using Hapi
server.route({
method: 'GET',
path: '/hello/world',
handler: function (request, h) {
return h.view('layout-hello-browserify', { hello: "world" });
}
});
////////////////////////////////////////////////////////////////
// Render layout-hello-browserify2.html template as 'hello/world2' page using Hapi
server.route({
method: 'GET',
path: '/hello/world2',
handler: function (request, h) {
return h.view('layout-hello-browserify2', { hello: "world" });
}
});
////////////////////////////////////////////////////////////////
// Load the current data for movies - stored in the file system
var movieData = Fs.readFileSync('public/data/movies.json');
var appData = {movies: JSON.parse(movieData)};
// Provide the bgColor helper, used by the movie-list template for striped rows. (
jsrender.views.helpers('bgColor', function() {
return this.index%2 ? '#fdfdfe' : '#efeff2';
}); // We could optionally have exposed the helper only to the the movie-list template - by passing
// in the template - jsrender.templates('./templates/moviespage.html') - as third parameter.
// (Note that JsRender server templates are automatically cached - so no additional call is made to the file-system)
// Define custom tags used in layout-movies.html template
jsrender.views.tags({
clientData: function(varName) { // Custom tag to render server data in a script block, so it can be used in the client without making an HTTP request
return '<script>var ' + varName + '=' + JSON.stringify(appData[varName]) + ';</script>';
}
});
////////////////////////////////////////////////////////////////
// Save updated data from client, received from POST request
server.route({
method: 'POST',
path: '/save/data',
handler: function (request, h) {
movieData = request.payload.movieData;
appData.movies = JSON.parse(movieData);
Fs.writeFileSync('./public/data/movies.json', movieData);
return "Saved...";
}
});
////////////////////////////////////////////////////////////////
// Start the server
await server.start();
console.log('Server running at:', server.info.uri);
};
start();