This repository was archived by the owner on Sep 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
83 lines (67 loc) · 2.5 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
const express = require('express');
const path = require("path");
const apiRouter = require("./routes/api");
const searchRouter = require("./routes/search");
const watchRouter = require("./routes/play");
const config = require("./config");
const cookieParser = require('cookie-parser')
const app = express();
const port = 3000;
app.use(express.json());
app.set("view engine", "ejs");
app.use(cookieParser())
app.use((req, res, next) => {
console.log(req.path);
if (req.path.startsWith("/api/auth")) {
next();
} else {
try {
if (config.auth.enabled) {
const authCode = req.cookies.authcode;
if (!authCode) return res.status(401).sendFile(path.join(__dirname, "static/pages/error", "401.html"));
if (authCode === config.auth.auth_code) {
next();
} else {
res.status(401).sendFile(path.join(__dirname, "static/pages/error", "401.html"));
}
} else {
next();
}
} catch (error) {
console.log(error);
}
}
});
app.use("/api", apiRouter); //ルートの読み込み
app.use((req, res, next) => {
if (req.url === "/") {
return res.sendFile(path.join(__dirname, "static/pages", "top.html"));
}
if (req.url.startsWith("/channel/")) {
return res.sendFile(path.join(__dirname, "static/pages", "channel.html"));
}
if (req.url === "/setting") {
return res.sendFile(path.join(__dirname, "static/pages", "setting.html"));
}
if (req.url.startsWith("/icons/")) {
const filename = req.url.split("/")[2] + ".svg"; // icons/[file]から[file]を取得
return res.sendFile(path.join(__dirname, "static/icons", filename));
}
if (req.url.startsWith("/styles/")) {
const filename = req.url.split("/")[2] + ".css"; // styles/[file]から[file]を取得
return res.sendFile(path.join(__dirname, "static/styles", filename));
}
next(); // 次のミドルウェアに処理を渡す
});
app.use("/search", searchRouter);
app.use("/watch", watchRouter);
app.use((req, res, err) => {
res.sendFile(path.join(__dirname, "static/pages/error", "404.html"));
console.log(err);
});
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
if (config.auth.enabled) {
console.log(`アクセスする前に認証が必要です。http://localhost:${port}/api/auth?c=${config.auth.auth_code}`);
}
});