forked from olivernn/augment.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
49 lines (41 loc) · 1.23 KB
/
server.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
/**
* Module dependencies.
*/
var http = require('http')
, url = require('url')
, join = require('path').join
, exists = require('path').exists
, extname = require('path').extname
, join = require('path').join
, fs = require('fs')
, port = process.argv[2] || 8003;
var mime = {
'html': 'text/html'
, 'css': 'text/css'
, 'js': 'application/javascript'
};
http.createServer(function(req, res){
console.log(' \033[90m%s \033[36m%s\033[m', req.method, req.url);
var pathname = url.parse(req.url).pathname
, path = join(process.cwd(), pathname);
function notFound() {
res.statusCode = 404;
res.end("404 Not Found\n")
}
function error(err) {
res.statusCode = 500;
res.end(err.message + "\n");
}
exists(path, function(exists){
if (!exists) return notFound()
fs.stat(path, function(err, stat){
if (err) return error();
if (stat.isDirectory()) path = join(path, 'index.html');
res.setHeader('Cache-Control', 'no-cache');
var parts = path.split('.')
res.setHeader('Content-Type', mime[parts[parts.length - 1]] || 'application/octet-stream');
fs.createReadStream(path).pipe(res);
});
})
}).listen(port);
console.log('\n Server listening on %d\n', port);