forked from spacewindow/robt2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
executable file
·106 lines (86 loc) · 2.58 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
var express = require("express");
var cors = require('cors');
var app = express();
const tinycolor = require("tinycolor2");
const { data } = require("./js/data.js");
app.set("port", process.env.PORT || 8080);
app.use(cors());
app.use(express.static(__dirname));
const color = {
black: "#222222",
white: "#ffffff",
palegrey: "#fafafa",
lightgrey: "#efefef",
midgrey: "#aaaaaa",
darkgrey: "#333333",
orange: "#ff9900"
};
// // get list of projects
// const fs = require('fs');
// let projects = [];
// fs.readdirSync('./views/projects').forEach(fileName => {
// projects.push(fileName.replace('.html.ejs', ''));
// });
// set the view engine to ejs
app.set("view engine", "ejs");
// default pageData
app.get("/", function(req, res) {
res.render("../views/index.html.ejs", {
allData: data,
color: color,
tinycolor: tinycolor
});
});
app.get("/djsb", function(req, res) {
res.render("../views/djsb/index.html.ejs");
});
app.get("/about", function(req, res) {
res.render("../views/about.html.ejs", {
allData: data,
color: color,
tinycolor: tinycolor
});
});
app.get("/video", function(req, res) {
res.render("../views/video.html.ejs", {
allData: data,
color: color,
tinycolor: tinycolor
});
});
// any page that is not the root / homepage
app.get("/fonts/segmdl2.woff", cors(), function (req, res){
res.json({msg:'This route is CORS enabled'});
});
app.get("/*", function(req, res) {
// if request is homepage
let pageData = {};
let reqPage = req.url.replace("/", "");
console.log("reqpage", reqPage);
// check if request is a page in the data
let dataIndex = data.findIndex(p => p.id === reqPage);
if (typeof dataIndex !== "undefined") {
// if the page exists, set up the data
pageData.current = data.filter(p => p.id === reqPage)[0];
pageData.previous = dataIndex > 0 ? data[dataIndex - 1] : null;
pageData.next = dataIndex < data.length - 1 ? data[dataIndex + 1] : null;
}
// render page. An unknown page will trigger express error
res.render("../views/projects/" + reqPage + ".html.ejs", {
pageData: pageData,
allData: data,
color: color,
tinycolor: tinycolor
});
});
// The above will fail with an error status (maybe 404), which will be handled below
// error handler
// app.use(function(err, req, res, next) {
// // render the error page
// res.status(err.status || 500);
// console.log(err.status); // coming up as undefined error... :(
// res.render('../views/404.html.ejs');
// });
app.listen(app.get("port"), function() {
console.log("Node app is running on port", app.get("port"));
});