-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
64 lines (50 loc) · 1.4 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
const express = require("express");
const path = require("path");
const fs = require("fs");
const connectDB = require("./database");
const port = 3000;
const app = express();
connectDB();
app.use(express.json());
app.use(express.static(__dirname));
app.get("/404", (req, res) => {
res.sendFile(path.join(__dirname, "/routes/topic/pages/404.html"));
});
app.get("/", (req, res) => {
res.sendFile(path.join(__dirname, "/routes/topic/pages/home.html"));
});
const auth = require("./api/auth");
app.use("/auth/api", auth);
app.get("/:folder/:page", (req, res) => {
const folder = req.params.folder;
const page = req.params.page;
const filePath = path.join(
__dirname,
"routes",
folder,
"pages",
`${page}.html`
);
fs.access(filePath, fs.constants.F_OK, (err) => {
if (err) {
res.redirect("/404");
} else {
res.sendFile(filePath);
}
});
});
app.get("/:page", (req, res) => {
const page = req.params.page;
const filePath = path.join(__dirname, "routes", "pages", `${page}.html`);
fs.access(filePath, fs.constants.F_OK, (err) => {
if (err) {
res.redirect("/404");
} else {
res.sendFile(filePath);
}
});
});
app.listen(port, (err) => {
if (err) return console.log(err);
console.log(`Doit project is listening on port ${port}`);
});