-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
112 lines (101 loc) · 2.72 KB
/
index.js
File metadata and controls
112 lines (101 loc) · 2.72 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import * as fs from 'node:fs';
import * as http from 'node:http';
import * as path from 'node:path';
const mediaTypes = {
"html": "text/html",
"jpeg": "image/jpeg",
"jpg": "image/jpeg",
"png": "image/png",
"svg": "image/svg+xml",
"json": "application/json",
"js": "text/javascript",
"css": "text/css",
"csv": "text/csv",
"mp3": "audio/mpeg",
"mp4": "video/mp4",
"oga": "audio/ogg",
"ogv": "video/ogg",
"pdf": "application/pdf",
"weba": "audio/webm",
"webm": "video/webm",
"webp": "image/webp",
"woff": "font/woff",
"woff2": "font/woff2",
"ttf": "font/ttf",
"gif": "image/gif"
};
const server = http.createServer((req, res) => {
const errorHTML = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Montserrat:wght@800&display=swap" rel="stylesheet">
<style>
body{
padding: 0; margin: 0;
font-family: 'Montserrat', sans-serif;
font-weight: 800;
background-color: #4343F9;
color: #fff;
}
#root{
width: 100%;
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
font-size: 21px;
}
</style>
<title>Not here</title>
</head>
<body>
<div id="root">Rise your gaze to the sky<br/>than a bit back to the URL bar<br/>and check that link again</div>
</body>
</html>
`;
if (req.method === "GET") {
let filePath = req.url === "/data.js" ? path.resolve(`${process.cwd()}/data.js`) : path.resolve(`${process.cwd()}/client${req.url}`);
fs.access(filePath, fs.constants.R_OK, (err) => {
if (err) {
res.statusCode = 404;
res.end(errorHTML);
} else {
if (fs.statSync(filePath).isDirectory()) {
filePath += '/index.html';
}
fs.readFile(filePath, (err, data) => {
if (err) {
res.statusCode = 500;
res.end(errorHTML);
} else {
let mediaType = mediaTypes[filePath.split('.').pop()];
if (!mediaType) {
mediaType = 'text/plain';
}
res.writeHead(200, { "Content-Type": mediaType });
res.write(data);
res.end();
}
});
}
});
}else if(req.method === "POST" && req.url === "/error"){
const chunks = [];
req.on('data', chunk => chunks.push(chunk));
req.on('end', () => {
const data = Buffer.concat(chunks);
console.log(data.toString());
});
res.end();
}
});
server.listen(9000, "127.0.0.1", () => {
const addr = server.address();
console.log(`Open this link in your browser: http://${addr.address}:${addr.port}`);
});