-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
41 lines (38 loc) · 1.14 KB
/
index.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
var http = require('http');
var fs = require('fs');
var path = require('path');
var url = require('url')
var mediaTypes = {
'.md' : 'text/markdown',
'.html' : 'text/html',
'.css' : 'text/css',
'.js' : 'application/javascript'
}
http.createServer(function (req, res) {
var pathname = url.parse(req.url).pathname.replace(/\.\.\//g,'');
var filename = path.resolve(pathname=='/' ? 'index.html' : pathname.substr(1));
console.log(filename)
path.exists(filename, function(exists) {
switch(exists) {
case true: return function(){
fs.readFile(filename, function(err,data) {
switch(err==undefined) {
case true: return function(){
res.writeHead(200,{'Content-Type': mediaTypes[path.extname(filename)]});
res.end(data);
}();
case false: return function(){
res.writeHead(500);
res.end();
}();
}
})
}();
case false: return function(){
res.writeHead(404);
res.end();
}();
}
});
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');