forked from wasedatime/wasedatime-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
95 lines (84 loc) · 2.39 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
require("dotenv").config({ path: "/var/www/.env" });
const express = require("express");
const helmet = require("helmet");
const timeout = require("connect-timeout");
const fs = require("fs");
const haltOnTimedout = (req, res, next) => {
if (!req.timedout) next();
};
const sendCustomizedIndexFile = (filePath, title, description) => {
return function (req, res, next) {
fs.readFile(filePath, "utf8", (err, data) => {
if (err) {
console.log(err);
res.sendFile(filePath);
} else {
data = data.replace(/%TITLE%/g, title);
data = data.replace(/%DESCRIPTION%/g, description);
res.send(data);
}
});
};
};
const app = express();
app.use(timeout("15s"));
app.use(helmet());
app.use(haltOnTimedout);
// process.env is read from the .env file on server
if (process.env.NODE_ENV === "production") {
//serve up production assests, disable directory indexing
app.use(express.static("client/build", { index: false }));
//serve up index.html if route is unrecognizable
const path = require("path");
const indexFilePath = path.join(__dirname, "client", "build", "index.html");
app.get(
"/",
sendCustomizedIndexFile(
indexFilePath,
"WasedaTime",
"An unofficial app for Syllabus Searching, Classroom Usage Checking, and Shuttle Bus Arrival Time Checking at Waseda University."
)
);
app.get(
"/timetable",
sendCustomizedIndexFile(
indexFilePath,
"WasedaTime - TimeTable",
"Create Your Own TimeTable at Waseda University."
)
);
app.get(
"/syllabus",
sendCustomizedIndexFile(
indexFilePath,
"WasedaTime - Syllabus",
"Syllabus Searching at Waseda University."
)
);
app.get(
"/roomfinder",
sendCustomizedIndexFile(
indexFilePath,
"WasedaTime - Classroom Usage",
"Classroom Usage Checking at Waseda University."
)
);
app.get(
"/bus",
sendCustomizedIndexFile(
indexFilePath,
"WasedaTime - Bus",
"Shuttle Bus Arrival Time Checking at Waseda University."
)
);
app.get(
"*",
sendCustomizedIndexFile(
indexFilePath,
"WasedaTime",
"An unofficial app for Syllabus Searching, Classroom Usage Checking, and Shuttle Bus Arrival Time Checking at Waseda University."
)
);
}
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => console.log(`App listening on port ${PORT}`));