-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
125 lines (108 loc) · 3.59 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/**
* @module server
* @file
* The main file of this server. It create access routes. To use it, you can write on a terminal : $ node server.js <br />
* Turn in javascript strict mode. <br />
*
* @version 1.0
* @since 1.0
*
* @requires path
* @requires handlebars
* @requires point-of-vew
* @requires fastify
* @requires fastify-static
* @requires fastify-formbody
*/
const port = 3000
const address = "127.0.0.1"
const path = require("path");
const Handlebars = require("handlebars");
// Require the fastify framework and instantiate it
const fastify = require("fastify")({logger: false});
// Setup our static files
fastify.register(require("fastify-static"), {
root: path.join(__dirname, "public"),
prefix: "/static/" // optional: default '/'
});
// fastify-formbody lets us parse incoming forms
fastify.register(require("fastify-formbody"));
// point-of-view is a templating manager for fastify
fastify.register(require("point-of-view"), {
engine: {
handlebars: require("handlebars")
},
root: path.join(__dirname, '/src/pages/'),
options: {
partials: {
htmlheader: 'partials/htmlheader.hbs',
nav: 'partials/nav.hbs',
footer: 'partials/footer.hbs'
}
}
});
// Load model associated functions
const model = require("./model.js");
// Load and parse page data
const pages = require("./src/text.json");
let pagesList = [{name: "quiz"}];
for (let key in pages) {
let page = {name: key,}
pagesList.push(page);
}
// add handlebars helpers
Handlebars.registerHelper('isEven', function (value) {
return value % 2;
});
Handlebars.registerHelper('capitalizeFirst', function (value) {
return value[0].toUpperCase() + value.substr(1);
});
Handlebars.registerHelper('isArray', function (value) {
return Array.isArray(value);
});
/**
* View Handling using Fastify
*/
fastify.get("/", function(request, reply) {
reply.header('Content-Type', 'text/html');
reply.view("index.hbs", {pagesList: pagesList});
});
fastify.get("/quiz", function(request, reply) {
reply.header('Content-Type', 'text/html');
reply.view("quiz.hbs", {pagesList: pagesList});
});
fastify.post("/quiz", function(request, reply) {
const criteria = request.body;
let matches = model.findChocolate(criteria.typ, criteria.preis, criteria.geschmack, criteria.inhalt);
reply.send({ matches: matches });
});
fastify.get("/impressum", function(request, reply) {
reply.header('Content-Type', 'text/html');
reply.view("impressum.hbs", {pagesList: pagesList});
});
// Serve top level pages dynamically
fastify.get("/*", function(request, reply) {
/**
* anonymous function dynamically serving top level pages
* @param {fastify.Request} request - the fastify request object
* @param {fastify.Reply} reply - the fastify reply object
*/
const targetPage = request.url.replace("/", "");
if (pages[targetPage]) {
//page does exist, return it
reply.header('Content-Type', 'text/html');
reply.view("pagetemplate.hbs", {page: pages[targetPage], pagesList: pagesList});
} else {
//page does not exist, send 404
reply.view("404.hbs", {target: targetPage, pagesList: pagesList});
}
});
// Run the server and report out to the logs
fastify.listen(port, address, function(err, address) {
if (err) {
fastify.log.error(err);
process.exit(1);
}
console.log(`Your app is listening on ${address}`);
fastify.log.info(`server listening on ${address}`);
});