This repository has been archived by the owner on Jul 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
109 lines (94 loc) · 3.4 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
'use strict';
const express = require('express')
const app = express()
const fs = require('fs');
let sourceData;
let desc;
desc = prepareMetadata();
sourceData = prepareSourceData();
let html = '';
fs.readFile(__dirname + '/dist/index.html', 'utf8', (err, data) => {
if (err) {
console.error(err);
return;
}
html = data;
});
app.use('/icon', express.static(__dirname + '/dist/icon/'));
app.use('/img', express.static(__dirname + '/dist/img/'));
app.use('/image', express.static(__dirname + '/dist/image/'));
app.use('/css', express.static(__dirname + '/dist/css/'));
app.use('/js', express.static(__dirname + '/dist/js/'));
app.use('/data', express.static(__dirname + '/dist/data/'));
app.get('/*', (req, res) => {
console.log("request received");
if (req.path === "/robots.txt") {
res.sendFile(__dirname + "/public/robots.txt");
return;
}
if (req.path === "/sitemap.xml") {
res.sendFile(__dirname + "/public/sitemap.xml");
return;
}
const d = pager(req.path);
let htmlTmp = html;
if (d) {
htmlTmp = html.replace("<title></title>", "<title>" + resolveTitle(d) + "</title>")
.replace("<meta content=\"\" name=\"description\"/>", "<meta name=\"description\" content=\"" + (d.description !== '' ? d.description : "22清陵祭公式HP") + "\"/>")
.replace("<meta content=\"\" property=\"twitter:title\"/>", "<meta property=\"twitter:title\" content=\"" + resolveTitle(d) + "\"/>");
}
res.header('Access-Control-Allow-Origin', 'https://shion1305.com')
res.send(htmlTmp);
});
const PORT = parseInt(process.env.PORT) || 8080;
app.listen(PORT, () => console.log('Example app listening on port ' + PORT));
function pager(entry) {
if (entry.indexOf(".") > -1) {
return {"title": "Not Found", "description": "お探しのページは見つかりませんでした"};
}
return resolveData(entry);
}
function resolveTitle(d) {
return (d.title !== "" ? d.title + " | 22清陵祭『花笑み』公式HP 横浜国立大学大学祭" : "22清陵祭『花笑み』公式HP 横浜国立大学大学祭")
}
function resolveData(entry) {
if (entry.indexOf(".") > -1) return;
if (!entry.endsWith("/")) {
entry = entry.concat("/");
}
console.log("ENTRY: " + entry);
const specialResult = processSpecial(entry);
if (specialResult) return specialResult;
return resolveNormal(entry);
}
function processSpecial(entry) {
const r = entry.match("^/events/(\\d{4})/?$");
if (!r) return null;
console.log(r[1]);
const r1 = sourceData.find((data) => data.id == r[1]);
console.log(r1);
return {
"title": r1.event_name + "(" + r1.org_name + ")の企画詳細",
"description": "22清陵祭オンライン企画『" + r1.event_name + "』(" + r1.org_name + ") の企画詳細ページです。" + r1.event_description
};
}
function resolveNormal(entry) {
console.log("resolveNormal");
return desc[entry];
}
function prepareMetadata() {
try {
return JSON.parse(fs.readFileSync(__dirname + '/description.json', 'utf8'));
} catch (err) {
console.log("error on loading metadata");
console.log(err);
}
}
function prepareSourceData() {
try {
return JSON.parse(fs.readFileSync(__dirname + "/src/assets/data.json", 'utf8'));
} catch (err) {
console.log("file load error");
console.log(err);
}
}