-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatic.js
More file actions
40 lines (34 loc) · 1.01 KB
/
static.js
File metadata and controls
40 lines (34 loc) · 1.01 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
var http = require('http');
var parse = require('url').parse;
var join = require('path').join;
var fs = require('fs');
var root = __dirname;
var server = http.createServer(function (req, res) {
console.log('server!');
var url = parse(req.url);
var path = join(root, url.pathname);
fs.stat(path, function (err, stat) {
if (err) {
if (err == 'ENOENT') {
res.statusCode = 404;
res.end('File Not Found');
}
else {
res.statusCode = 500;
res.end('Internal Server Error');
}
}
else {
var stream = fs.createReadStream(path);
res.setHeader('Content-Length', stat.size);
stream.pipe(res);
stream.on('error', function (err) {
res.statusCode = 500;
res.end('Internal Server Error');
});
}
});
});
server.listen(9000, null, null, function(){
console.log('listening on 9000');
});