-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathserver.js
49 lines (43 loc) · 1.24 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
const path = require("path");
const fs = require("fs");
const http = require("http");
const PORT = 8080;
const globalController = (req, res, filePath) => {
var extname = String(path.extname(filePath)).toLowerCase();
var mimeTypes = {
".html": "text/html",
".js": "text/javascript",
".css": "text/css",
".json": "application/json",
".png": "image/png",
".jpg": "image/jpg",
".gif": "image/gif",
".svg": "image/svg+xml",
".wav": "audio/wav",
".mp4": "video/mp4",
".woff": "application/font-woff",
".ttf": "application/font-ttf",
".eot": "application/vnd.ms-fontobject",
".otf": "application/font-otf",
".wasm": "application/wasm",
};
var contentType = mimeTypes[extname] || "application/octet-stream";
res.writeHead(200, { "Content-Type": contentType });
fs.readFile(`.${filePath}`, (err, data) => {
if (err) {
return console.error(err);
}
return res.end(data, "utf-8");
});
console.log(filePath);
};
const server = http.createServer((req, res) => {
let filePath = req.url;
if (filePath === "/") {
filePath = "/index.html";
}
globalController(req, res, filePath);
});
server.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});