-
Notifications
You must be signed in to change notification settings - Fork 0
/
requestHandlers.js
45 lines (39 loc) · 1.03 KB
/
requestHandlers.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
"use strict";
var fs = require('fs'),
path = require("path");
var types = {
".js": "application/javascript",
".css": "text/css",
".json": "text/plain",
".png": "image/png",
".jpeg": "image/jpeg",
".jpg": "image/jpg",
".svg": "image/svg+xml"
};
function index(res) {
fs.readFile("index.html", "utf8", function (err, data) {
res.writeHead(200, {"Content-Type": "text/html"});
if (err) {
throw err;
} else {
res.write(data);
}
res.end();
});
}
function data(res, filePath) {
readFile(res, filePath.substring(1), types[path.extname(filePath)]);
}
function readFile(res, filePath, fileType) {
res.writeHead(200, {'Content-Type': fileType });
if (fileType.substring(0, 5) === 'image') {
res.end(fs.readFileSync(filePath), "binary");
} else {
fs.readFile(filePath, "utf8", function(err, data) {
if (err) throw err;
res.end(data);
});
}
}
exports.index = index;
exports.data = data;