/
connect-livepage.js
44 lines (36 loc) · 1.08 KB
/
connect-livepage.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
(function() {
var cUtils, fs, mime, parse, path, _types;
fs = require('fs');
parse = require("url").parse;
path = require("path");
mime = require('mime');
cUtils = (require("connect")).utils;
_types = ['text/html', 'text/css', 'application/javascript'];
exports.connect = function(root) {
return function(req, res, next) {
var contentType, filepath, pathname;
switch (req.method.toUpperCase()) {
case "HEAD":
pathname = parse(req.url).pathname;
contentType = mime.lookup(pathname);
if (_types.indexOf(contentType) >= 0) {
filepath = path.join(root, pathname);
return fs.stat(filepath, function(err, stats) {
if (err == null) {
res.writeHead(200, {
'Content-Type': contentType,
'Etag': cUtils.etag(stats)
});
}
return res.end();
});
} else {
return next();
}
break;
default:
return next();
}
};
};
}).call(this);