-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathfile-server-md5.js
More file actions
32 lines (28 loc) · 882 Bytes
/
file-server-md5.js
File metadata and controls
32 lines (28 loc) · 882 Bytes
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
var util = require("util"),
http = require("http"),
url = require("url"),
path = require("path"),
crypto = require("crypto"),
fs = require("fs");
var dir = process.argv[2] || './public';
util.log('Serving files from ' + dir);
http.createServer(function(request, response) {
var uri = url.parse(request.url).pathname;
var filename = path.join(process.cwd(), dir, uri);
util.log('Serving file ' + filename);
path.exists(filename, function(exists) {
if(exists) {
fs.readFile(filename, function(err, data) {
var hash = crypto.createHash('md5');
hash.update(data);
response.writeHead(200, { 'Content-Type': 'text/plain',
'Content-MD5': hash.digest('base64') });
response.end(data);
});
} else {
response.writeHead(404);
response.end();
}
});
}).listen(8080);
util.log("Server running at http://localhost:8080/");