-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
120 lines (102 loc) · 2.8 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
const express = require('express');
const hbs = require('hbs');
const fs = require('fs');
// heroku will set an env PORT value
const PORT = process.env.PORT || 3000;
// set to true to route traffic to maintenance page
const MAINTENANCE = false;
var app = express();
// partials are blocks of very reusable html/hbs code
hbs.registerPartials(__dirname + '/views/partials');
// helpers are essentially used the same way as the variables
// but they execute a function
hbs.registerHelper('getCurrentYear', () => {
return new Date().getFullYear();
});
// set templating engine to handlebars
app.set('view engine', 'hbs');
// using middleware to add functionality
// NEED next(), this all runs before app renders
app.use((req, res, next) => {
var now = new Date().toString();
var log = `${now}: ${req.method} ${req.url}`;
console.log(log);
fs.appendFile('server.log', log + '\n', (err) => {
if (err) {
console.log('Unable to append to server.log');
}
});
next();
});
if (MAINTENANCE) {
//interrupt site and render maintenance page
app.use((req, res, next) => {
res.send('<h1 style="text-align:center; padding-top:10%;">Sorry!</h1> <h2 style="text-align:center">Site is down for maintenance. We\'ll be back up soon!</h2>');
});
}
var GAME = {condition: false};
// using a static directory instead of handlers
app.use(express.static(__dirname + '/static'));
// handlers galore!
app.get('/', (req, res) => {
res.render('home.hbs', {
aboutActive: '',
gameProcessActive: '',
gameInfoActive: '',
visionActive: '',
GAME: encodeURIComponent(JSON.stringify(GAME))
});
});
app.get('/about', (req, res) => {
res.render('about.hbs', {
aboutActive: 'active',
gameInfoActive: '',
gameProcessActive: '',
visionActive: '',
GAME: encodeURIComponent(JSON.stringify(GAME))
});
});
app.get('/game', (req, res) => {
res.render('game.hbs', {
aboutActive: '',
gameInfoActive: '',
gameProcessActive: '',
visionActive: '',
GAME: encodeURIComponent(JSON.stringify(GAME))
});
});
app.get('/gameInfo', (req, res) => {
res.render('gameInfo.hbs', {
aboutActive: '',
gameInfoActive: 'active',
gameProcessActive: '',
visionActive: '',
GAME: encodeURIComponent(JSON.stringify(GAME))
});
});
app.get('/gameProcess', (req, res) => {
res.render('gameProcess.hbs', {
aboutActive: '',
gameInfoActive: '',
gameProcessActive: 'active',
visionActive: '',
GAME: encodeURIComponent(JSON.stringify(GAME))
});
});
app.get('/vision', (req, res) => {
res.render('vision.hbs', {
aboutActive: '',
gameInfoActive: '',
gameProcessActive: '',
visionActive: 'active',
GAME: encodeURIComponent(JSON.stringify(GAME))
});
});
// app.get('/bad', (req, res) => {
// res.send({
// errorMsg: 'ERROR: Unable to handle request'
// });
// });
app.listen(PORT, () => {
console.log(`Server is up on port ${PORT}`);
});