-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
93 lines (74 loc) · 2.18 KB
/
index.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
import express from "express";
import exerciseStringList from "./public/exercises/index.cjs";
import fileController from "./server/api/files.js";
const app = express();
let myIp = `69.181.248.93`;
/*
fetch("https://ifconfig.me/all.json")
.then((r) => r.json())
.then((r) => {
myIp = r.ip_addr;
});
*/
const externalPort = `8035`;
const ipUrl = `${myIp}:${externalPort}`;
const getReqIp = (req) => {
const initialIp = req.get("x-forwarded-for") || req.ip || "";
return initialIp.replace("::ffff:", "");
};
app.use(express.static("dist"));
app.use(express.static("public"));
app.use(express.json());
app.use("/api/files", fileController);
const exampleLocalIp = "192.168.1.23";
app.get(["/", "/:article"], (req, res) => {
/*
// Source: https://stackoverflow.com/questions/38423930/how-to-retrieve-client-and-server-ip-address-and-port-number-in-node-js
console.log("req.connection", req.connection.remoteAddress);
console.log("req.connection", req.connection.remotePort);
console.log("req.connection", req.connection.localAddress);
console.log("req.connection", req.connection.localPort);
*/
const reqIp = getReqIp(req);
console.log("hostname?", req.hostname);
const hostname = (req.hostname || "").trim();
const articleName =
hostname === myIp
? "ip-request-response-example"
: req.params.article || "what-happens-when-you-buy-internet";
let variables = {
ipUrl,
myIp,
reqIp,
exampleLocalIp,
externalPort,
articleName,
hostname,
serverTS: Date.now(),
};
res.send(`
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" href="https://fonts.xz.style/serve/inter.css">
<link rel="stylesheet" href="/main.css">
</head>
<body>
<div id="root"></div>
<script>
const markdocVariableString = '${JSON.stringify(variables)}'
const mdContentPath = '/articles/${articleName}.md'
const markdocExercises = ${exerciseStringList[articleName]};
const changeTheme = (theme='cupcake') => {
document.documentElement.dataset.theme = theme
}
// Themes: https://daisyui.com/docs/themes/
</script>
<script src="/main.js"></script>
</body>
</html>
`);
});
app.listen(process.env.PORT || 8123);