/
handler.js
73 lines (55 loc) · 2.02 KB
/
handler.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
62
63
64
65
66
67
68
69
70
71
72
73
var fs = require('fs');
var path = require('path');
var processDirectory = require('./processDirectory');
var processFile = require('./processFile');
var suffixes = ['/index.md', '/index.markdown', '', '.md', '.markdown'];
module.exports = function(wwwRoot) {
wwwRoot = path.resolve(wwwRoot);
processDirectory = processDirectory(wwwRoot);
processFile = processFile(wwwRoot);
return function(req, res) {
var handled, processed = suffixes.map(function(){ return false;});
function checkDone() {
if (handled) { return; }
for (var i = 0; i < processed.length; i++) {
if (processed[i] && processed[i].stat) { break; }
}
var allError = processed.every(function(r) { return r && r.err; });
if (suffixes.length === i && allError) {
res.writeHead(404, { 'content-type': 'text/plain' });
res.end('not found');
return;
}
var use = processed[i];
if (!use) { return; }
handled = true;
if (use.stat.isFile()) {
return processFile(use.file, res);
}
return processDirectory(use.file, res);
}
var reqPath = path.resolve(wwwRoot, './' + req.url);
if (reqPath.indexOf(wwwRoot) !== 0) {
res.writeHead(500, { 'content-type': 'text/plain' });
res.end('do not want');
return;
}
if (path.basename(reqPath)[0] === '.') {
res.writeHead(500, { 'content-type': 'text/plain' });
res.end('do not want');
return;
}
suffixes.map(function(suffix) {
return reqPath + suffix;
}).forEach(function(reqPath, index) {
fs.stat(reqPath, function(err, stat) {
processed[index] = {
file: reqPath,
err: err,
stat: stat
};
checkDone();
});
});
};
};