-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
86 lines (86 loc) · 3.78 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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const http_1 = require("http");
const fs_1 = require("fs");
const database_1 = require("./database/database");
(0, http_1.createServer)((req, res) => {
if (!req.url)
return res.end((0, fs_1.readFileSync)("pages/comingSoon.html"));
const url = new URL(req.url, `http://${req.headers.host}`);
/** Request pathname without leading `/` or trailing `.html` */
const path = url.pathname.endsWith(".html")
? url.pathname.slice(1, -5)
: url.pathname.slice(1);
let data = "";
req.on("data", (chunk) => {
data += chunk;
});
req.on("end", () => {
if (req.headers["content-length"] && !(Number.parseInt(req.headers["content-length"]) === data.length)) {
console.error("Request body length does not match content-length header");
return res.end("Request body length does not match content-length header");
}
if (req.method === "GET") {
if (path === "") {
// If the request is for the root path, show coming soon
res.writeHead(200, { "Content-Type": "text/html" });
res.end((0, fs_1.readFileSync)("pages/comingSoon.html"));
}
else if (path.endsWith(".js")) {
// If the request is for a JavaScript file, serve it
if ((0, fs_1.existsSync)(path)) {
res.writeHead(200, { "Content-Type": "application/javascript" });
res.end((0, fs_1.readFileSync)(path));
}
else {
res.writeHead(404, { "Content-Type": "text/plain" });
res.end("File not found");
console.error("File not found: " + path);
}
}
else if (path.endsWith(".css")) {
// If the request is for a CSS file, serve it
if ((0, fs_1.existsSync)(path)) {
res.writeHead(200, { "Content-Type": "text/css" });
res.end((0, fs_1.readFileSync)(path));
}
else {
res.writeHead(404, { "Content-Type": "text/plain" });
res.end("File not found");
console.error("File not found: " + path);
}
}
else if (!(0, fs_1.existsSync)(`pages/${path}.html`)) {
// If the requested page doesn't exist, show coming soon w/ 404
res.writeHead(404, { "Content-Type": "text/html" });
res.end((0, fs_1.readFileSync)("pages/comingSoon.html"));
}
else {
// Otherwise, show the requested page
res.writeHead(200, { "Content-Type": "text/html" });
res.end((0, fs_1.readFileSync)(`pages/${path}.html`));
}
}
else if (req.method === "POST") {
if (path.startsWith("database/")) {
try {
(0, database_1.sendToDatabase)((0, database_1.convertFormEncodedToClass)(data));
res.writeHead(200, { "Content-Type": "text/plain" });
res.end("Success");
}
catch (err) {
console.error(err);
res.writeHead(500, { "Content-Type": "text/plain" });
res.write("An error occurred while sending your data to the database");
res.end("\n\n" + err);
}
}
}
else {
// If unrecognized method, show coming soon w/ 400
res.writeHead(400, { "Content-Type": "text/html" });
res.end((0, fs_1.readFileSync)("pages/comingSoon.html"));
}
});
}).listen(8080);
//# sourceMappingURL=index.js.map