-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
61 lines (61 loc) · 1.83 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
import { createServer } from "node:http";
import { networkInterfaces } from 'os';
import { readFile } from "node:fs/promises";
const PORT = 8080;
// Define a map of files to serve
const files = {
"/PatreonScript.js": {
content: await readFile("PatreonScript.js"),
type: "application/javascript",
},
"/PatreonConfig.json": {
content: await readFile("PatreonConfig.json"),
type: "application/json",
},
"/PatreonIcon.png": {
content: await readFile("PatreonIcon.png"),
type: "image/png",
},
};
function getLocalIPAddress() {
const br = networkInterfaces();
const network_devices = Object.values(br);
if (network_devices !== undefined) {
for (const network_interface of network_devices) {
if (network_interface === undefined) {
continue;
}
for (const { address, family } of network_interface) {
if (family === "IPv4" && address !== "127.0.0.1") {
return address;
}
}
}
}
throw new Error("panic");
}
createServer((req, res) => {
const file = (() => {
switch (req.url) {
case "/PatreonScript.js":
return files[req.url];
case "/PatreonConfig.json":
return files[req.url];
case "/PatreonIcon.png":
return files[req.url];
default:
return undefined;
}
})();
if (file !== undefined) {
res.writeHead(200, { "Content-Type": file.type });
res.end(file.content);
return;
}
res.writeHead(404);
res.end("File not found");
return;
}).listen(PORT, () => {
console.log(`Server running at http://${getLocalIPAddress()}:${PORT}/PatreonConfig.json`);
});
//# sourceMappingURL=server.js.map