-
Notifications
You must be signed in to change notification settings - Fork 222
Expand file tree
/
Copy pathhandler.js
More file actions
43 lines (40 loc) · 1.07 KB
/
handler.js
File metadata and controls
43 lines (40 loc) · 1.07 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
const fs = require("fs");
const path = require("path");
const homeHandler = (request, response) => {
const filePath = path.join(__dirname, "../public/index.html");
fs.readFile(filePath, (error, file) => {
if (error) {
return console.log(error);
response.writeHead(500, { "Content-Type": "text/html" });
response.end("server error");
} else {
response.writeHead(200, {
"Content-Type": "text/html"
});
response.end(file);
}
});
};
const publicHandler = (request, response, endpoint) => {
const extinsion = endpoint.split(".")[1];
const extinsionType = {
html: "text/html",
css: "text/css",
js: "application/javascript",
jpg: "image/jpg",
ico: "image/x-icon"
};
const filePath = path.join(__dirname, "/../public", endpoint);
fs.readFile(filePath, (error, file) => {
if (error) {
response.writeHead(500, { "Content-Type": "text/html" });
response.end("server error");
} else {
response.writeHead(200, {
"Content-Type": extinsionType[extinsion]
});
response.end(file);
}
});
};
module.exports = { homeHandler, publicHandler };