-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
65 lines (50 loc) · 1.97 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
62
63
64
65
import { exec } from "child_process";
import { createReadStream } from "fs";
import { access, readFile } from "fs/promises";
import { createServer } from "http";
import os from "os";
import { dirname, extname, join } from "path";
import { fileURLToPath } from "url";
const PORT = process.env.PORT ?? 3000;
const pkg = await readFile("./package.json").then(JSON.parse);
const basePathname = new URL(pkg.homepage).pathname.split("/")[1];
const baseUrl = `http://localhost:${PORT}/${basePathname == "" ? "" : `${basePathname}/`}`;
const fileExtensionToMimeTypeMap = new Map()
.set("css", "text/css; charset=UTF-8")
.set("html", "text/html; charset=UTF-8")
.set("js", "application/javascript");
createServer(async (req, res) => {
const STATIC_PATH = dirname(fileURLToPath(import.meta.url));
const url = req.url
.split("/")
.filter((part) => part != basePathname)
.join("/");
const pathParts = [STATIC_PATH, url];
if (url.endsWith("/")) pathParts.push("index.html");
let filePath = join(...pathParts);
const exists = await access(filePath).then(
() => true,
() => false,
);
const sendText = (statusCode, text) => {
res.writeHead(statusCode, { "Content-Type": "text/html; charset=utf-8" });
res.end(text);
};
const fileExtension = extname(filePath).substring(1).toLowerCase();
let mimeType = fileExtensionToMimeTypeMap.get(fileExtension);
if (fileExtension && !mimeType) return sendText(501, "Not implemented");
if (!exists) {
if (mimeType) return sendText(404, "Not found");
filePath = join(STATIC_PATH, "404.html");
mimeType = "html";
}
const stream = createReadStream(filePath);
res.writeHead(200, { "Content-Type": mimeType });
stream.pipe(res);
}).listen(PORT, () => {
console.info(`Server started on ${baseUrl}`);
const platform = os.platform();
if (platform === "darwin") exec(`open ${baseUrl}`);
if (platform === "linux") exec(`xdg-open ${baseUrl}`);
if (platform === "win32") exec(`start ${baseUrl}`);
});