-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdispatcher.js
61 lines (51 loc) · 1.59 KB
/
dispatcher.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
const fs = require("fs");
const path = require("path");
const defaultRoot = "index.html";
const basePath = "./src";
// url to to dynamic pages and static resources
// (defaults to index.html)
const getLocalPath = (url) => {
const requestDir = basePath + url
return (requestDir == `${basePath}/`)
? requestDir + defaultRoot
: basePath + url
}
// reduce resource Content-Type
const getContentType = (extname) => {
const contentTypes = {
".html": "text/html",
".css": "text/css",
".js": "text/javascript",
"json": "text/json",
"svg": "image/svg+xml",
"ico": "image/x-icon",
"mp4": "video/mp4",
"default": "text/html"
}
return contentTypes[extname] || contentTypes["default"]
}
// Compose response for dynamic dynamic pages and static resources and file types
const dispatch = (request, response) => {
const filePath = getLocalPath(request.url)
const contentType = getContentType(path.extname(filePath))
fs.readFile(filePath, (err, content) => {
if (err) {
const { syscall, code } = err
console.error(`When trying to ${syscall} "${filePath}" receved ${code} error. ${(code === "ENOENT") ? `ENOENT (file doesnt exist)` : code}`)
}
if (err) {
response.writeHead(500);
response.end();
} else {
response.writeHead(200, {
"Content-Type": contentType
});
response.end(content, "utf-8");
}
})
}
module.exports = {
dispatch,
getLocalPath,
getContentType
}