-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
68 lines (68 loc) · 2.13 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
65
66
67
68
const http = require("http");
const { readFileSync } = require("fs");
const path = require("path");
// Create a server using `http`
const server = http.createServer((req, res) => {
console.log(`Incoming Request - Method: ${req.method} | URL: ${req.url}`);
// Process the body of the request
let reqBody = "";
req.on("data", (data) => {
reqBody += data;
});
// When the request is finished processing the entire body
req.on("end", () => {
// Parsing the body of the request
if (reqBody) {
req.body = reqBody
.split("&")
.map((keyValuePair) => keyValuePair.split("="))
.map(([key, value]) => [key, value.replace(/\+/g, " ")])
.map(([key, value]) => [key, decodeURIComponent(value)])
.reduce((acc, [key, value]) => {
acc[key] = value;
return acc;
}, {});
}
// Home Page
if (req.method === "GET" && req.url === "/") {
const resBody = readFileSync("./public/index.html");
res.statusCode = 200;
res.setHeader("Content-Type", "text/html");
res.end(resBody);
return;
}
// Serving Static Assets
const ext = path.extname(req.url);
if (req.method === "GET" && ext) {
try {
const resBody = readFileSync('.' + "/public" + req.url);
res.statusCode = 200;
if (ext === ".jpg" || ext === ".jpeg") {
res.setHeader("Content-Type", "image/jpeg");
} else if (ext === ".css") {
res.setHeader("Content-Type", "text/css");
} else if (ext === ".js") {
res.setHeader("Content-Type", "text/javascript");
}
res.end(resBody);
return;
} catch {
console.error(
"Cannot find asset",
path.basename(req.url),
"in assets folder"
);
}
}
// Page Not Found
res.statusCode = 404;
res.setHeader("Content-Type", "text/plain");
const resBody = "Page Not Found";
res.write(resBody);
res.end();
});
});
// Set the port to 5000
const port = 5000;
// Tell the port to listen for requests on localhost:5000
server.listen(port, () => console.log("Server is running on port", port));