-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.cjs
More file actions
71 lines (66 loc) · 2.08 KB
/
server.cjs
File metadata and controls
71 lines (66 loc) · 2.08 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
var http = require("http");
const requestIp = require("@supercharge/request-ip");
var fs = require("fs");
var path = require("path");
http
.createServer(function (request, response) {
const ip = requestIp.getClientIp(request);
console.log(`request: url=>${request.url}, ip=${ip}`);
var filePath = "." + request.url;
if (filePath == "./") {
filePath = "./index.html";
}
var extname = String(path.extname(filePath)).toLowerCase();
if (extname == "") {
try {
fs.access(filePath, fs.constants.R_OK);
console.log("file " + filePath + " exists.");
} catch {
console.log("file " + filePath + " doesn't exist.");
filePath += ".html";
extname = ".html";
}
}
var mimeTypes = {
".css": "text/css",
".eot": "application/vnd.ms-fontobject",
".gif": "image/gif",
".html": "text/html",
".jpg": "image/jpg",
".json": "application/json",
".js": "text/javascript",
".mjs": "text/javascript",
".mp4": "video/mp4",
".otf": "application/font-otf",
".pdf": "application/pdf",
".png": "image/png",
".svg": "image/svg+xml",
".ttf": "application/font-ttf",
".wasm": "application/wasm",
".wav": "audio/wav",
".woff": "application/font-woff",
};
var contentType = mimeTypes[extname] || "application/octet-stream";
fs.readFile(filePath, function (error, content) {
if (error) {
if (error.code == "ENOENT") {
fs.readFile("./404.html", function (error, content) {
response.writeHead(404, { "Content-Type": "text/html" });
response.end(content, "utf-8");
});
} else {
response.writeHead(500);
response.end(
"Sorry, check with the site admin for error: " +
error.code +
" ..\n"
);
}
} else {
response.writeHead(200, { "Content-Type": contentType });
response.end(content, "utf-8");
}
});
})
.listen(80);
console.log("Server running at http://127.0.0.1:80/");