-
Notifications
You must be signed in to change notification settings - Fork 0
/
dev_server.js
85 lines (71 loc) · 2.2 KB
/
dev_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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import fs from "fs";
import http from "http";
import url from "url";
import path from "path";
import { fileURLToPath } from "url";
import mime from "mime";
// const __filename = fileURLToPath(import.meta.url);
// const __dirname = path.dirname(__filename);
// console.log("directory-name 👉️", __dirname);
// console.log("file-name 👉️", __filename);
const jsonFile = fs.readFileSync(`./package.json`);
const jsonParsed = JSON.parse(jsonFile);
let homepageURL = jsonParsed.homepage;
if (!homepageURL.endsWith("/")) {
homepageURL += "/";
}
const urlPostFix = url.parse(homepageURL).path;
const urlPostFixWithoutSlash = urlPostFix.substring(0, urlPostFix.length - 1);
// console.log(homepageURL.href);
// console.log(jsonParsed.url);
const server = http.createServer((req, res) => {
const currentURL = new url.URL(req.url, `http://${req.headers.host}`);
if (currentURL.pathname === urlPostFixWithoutSlash) {
res.writeHead(301, {
location: urlPostFix,
});
res.end();
return;
}
if (!currentURL.pathname.startsWith(urlPostFix)) {
res.writeHead(404, { "Content-type": "text/html" });
res.end("404 page not found", "utf-8");
return;
}
const newPathname = currentURL.pathname.substring(urlPostFix.length - 1);
let filePath = "." + newPathname;
if (filePath === "./") {
filePath = "./index.html";
}
fs.readFile(filePath, (err, content) => {
if (err) {
console.log("ERROR OCCURRED! for filepath: " + filePath);
const handleContent = (content) => {
res.writeHead(404, { "Content-type": "text/html" });
res.end(content, "utf-8");
};
if (fs.existsSync(`./404.html`)) {
handleContent(fs.readFileSync("./404.html"));
} else {
handleContent("404 page not found");
}
return;
}
let contentType = mime.getType(filePath);
if (!contentType) {
contentType = "text/html";
}
res.writeHead(200, { "Content-type": contentType });
res.end(content, "utf-8");
});
});
server.listen(1337, "127.0.0.1", () => {
const serverAddress = server.address();
console.log(
"Server started at " +
serverAddress.address +
":" +
serverAddress.port +
urlPostFix
);
});