-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
134 lines (117 loc) · 3.66 KB
/
app.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
126
127
128
129
130
131
132
133
134
// imports - must use EcmaScript syntax
const express = require('express');
const path = require('path');
const Portfolio = require('./portfolio');
const links = require('./redirect-links.json');
const { create } = require('express-handlebars');
const { redirect } = require('express/lib/response');
// initalize app
const app = express();
const PORT = process.env.PORT || 5000;
const hbs = create({
// Specify helpers which are only registered on this instance.
helpers: {
bar() {
return foo;
},
},
});
//express handlebars middleware
app.engine('handlebars', hbs.engine);
app.set('view engine', 'handlebars');
app.set('views', './views');
// set public folder as static
app.use(express.static(path.join(__dirname, 'public')));
// access public files for CSS, JS, Favicons, and Images
app.use('/css', express.static(path.join(__dirname, 'public/css')));
app.use('/js', express.static(path.join(__dirname, 'public/js')));
app.use('/img', express.static(path.join(__dirname, 'public/img')));
// enable routing
app.use(express.urlencoded({ extended: false }));
var topThreeProjects = new Array(3);
Portfolio.portfolio.forEach((elem) => {
switch (elem.title) {
case 'Tobor Party':
topThreeProjects[0] = elem;
break;
case 'Hole Flounder':
topThreeProjects[1] = elem;
break;
case 'Time DashKing':
topThreeProjects[2] = elem;
break;
}
});
// routing
const staticPages = require('./static-data.js').staticPages;
for (const page in staticPages) {
if (page === 'home') {
app.get('/', (_, res) => {
res.render(page, staticPages[page]);
});
} else {
app.get('/' + page, (_, res) => {
res.render(page, staticPages[page]);
});
}
}
// project routing
Portfolio.portfolio.forEach((elem) => {
if (elem.permalink) {
// project page routing
app.get('/' + elem.permalink, (_, res) =>
res.render('project', {
project: elem,
styles: ['style.css', 'proj.css', 'tabletsupport.css'],
scripts: ['project.js'],
})
);
// add webGL routing (if it exists)
if (elem.builds) {
app.use(
'/' + elem.permalink + '/builds',
express.static(elem.buildsPath)
);
if (elem.builds['WebGL']) {
app.get('/' + elem.permalink + '/WebGL', (_, res) =>
res.sendFile(path.join(elem.builds['WebGL'], 'index.html'))
);
}
}
// make all images static
if (elem.images)
app.use(
'/' + elem.permalink + '/img',
express.static(elem.imagesPath)
);
}
});
// get request redirect for other sites
links.forEach((link) => {
app.get('/' + link.title, (_, res) => res.redirect(link.redirect));
});
// redirection adjustment handlers
app.get('/portfolio/', (_, res) => res.redirect('/portfolio'));
app.get('/home', (_, res) => res.redirect('/'));
// redirect games and projects with proper index
app.get('/games', (_, res) => {
res.cookie('index', '0');
res.redirect('/portfolio');
});
app.get('/projects', (_, res) => {
res.cookie('index', '1');
res.redirect('/portfolio');
});
// 404 middleware
app.use((_, res) => {
res.status(404).render('404', {
styles: ['style.css', '404.css', 'tabletsupport.css'],
scripts: ['error404Handler.js'],
});
});
// activate on PORT
app.listen(PORT, () => {
return console.log(
`Server started on PORT ${PORT} \nAccess on http://localhost:${PORT}`
);
});