-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
79 lines (74 loc) · 2.12 KB
/
app.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
var http = require('http');
var fs = require('fs');
var url = require('url');
http.createServer(function(request,response){
var requestUrl = url.parse(request.url);
var pathname = requestUrl.pathname;
var ext = pathname.match(/(\.[^.]+|)$/)[0];
var isExampleSource = /\/example/.test(pathname);
switch(ext){
case '.css':
case '.js':
var _file = isExampleSource ? './' + request.url : './dest/' + request.url;
fs.readFile(_file,'utf-8',function(err,data){
if(err){
return false;
throw err;
}
response.writeHead(200,{
"Content-Type":{
".css" : "text/css",
".js" : "application/javascript",
}[ext]
});
response.write(data);
response.end();
});
break;
case '.ico':
response.writeHead('404',{'Content-Type':'text/plain'});
response.end('Not Found');
break;
case '.png':
case '.jpg':
fs.readFile('./' +request.url,function(err,data){
if(err){
throw err;
}
response.writeHead(200,{
'Content-Type':'image/*'
});
response.write(data);
response.end();
});
break;
case '.json':
fs.readFile('./' + request.url,function(err,data){
if(err){
throw err;
}
response.writeHead('200');
response.end(data);
})
break;
default:
var _file = isExampleSource ? './' + request.url : './dest/' + (pathname !== '/' ? pathname : '/index.html');
function readFile(file){
fs.readFile(file,'utf-8',function(err,data){
if(err){
if(file === './dest/index.html'){
response.writeHead('404',{'Content-Type':'text/plain'});
response.end('404 Not Found');
return;
}
file = './dest/index.html';
return readFile(file);
}
response.writeHead(200,{'Content-Type':'text/html'});
response.write(data);
response.end();
})
}
readFile(_file);
}
}).listen(3000);