Repository navigation
Expand file tree
/
Copy pathrequestHandlers.js
More file actions
67 lines (57 loc) · 1.95 KB
/
Copy pathrequestHandlers.js
File metadata and controls
67 lines (57 loc) · 1.95 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
var fs = require("fs");
var mime = require("mime");
var util = require("util");
var walk = function(dir) {
var results = []
var list = fs.readdirSync(dir)
list.forEach(function(file) {
file = dir + '/' + file
var stat = fs.statSync(file)
if (stat && stat.isDirectory())
results = results.concat(walk(file))
else
results.push(file)
})
return results
}
var handlersMap = {};
// Generating handlers for /frontend/public (stuff like css, client js should be available by default)
var publicFiles = walk("frontend/public");
publicFiles.forEach(function(file) {
var clientPath = file.substr(("frontend/public").length);
handlersMap[clientPath] = function(response, request) {
fs.stat(file, function(err, data) {
if (err)
throw err;
var lastModReq = request.headers["if-modified-since"];
if (lastModReq)
lastModReq = new Date(lastModReq).getTime();
if (Math.floor(data.mtime.getTime() / 1000) ==
Math.floor(lastModReq) / 1000) {
response.writeHead(304, {
"Last-modified": data.mtime.toUTCString()
});
response.end();
return;
} else {
response.writeHead(200, {
"Content-type": mime.lookup(file),
"Last-modified": data.mtime.toUTCString()
});
fs.readFile(file, "utf8", function(err, data) {
response.end(data);
})
}
})
}
})
// Loading custom handlers
var handlers = walk("handlers");
handlers.forEach(function(handlerPath) {
util.log("Loading handler", handlerPath);
var handler = require("./" + handlerPath);
for (var path in handler.handlersMap) {
handlersMap[path] = handler.handlersMap[path];
}
})
exports.handlersMap = handlersMap;