-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
88 lines (71 loc) · 1.67 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
80
81
82
83
84
85
86
87
88
var http = require("http"), url = require("url"), path = require("path"), fs = require("fs");
http.createServer(function(req, res){
if(req.method != "GET")
return;
var uri = url.parse(req.url).pathname;
var file_name = path.join(process.cwd(), uri);
var match = file_name.match(/\.([a-z0-9]+)$/), content_type;
if(match){
var ext = match[1];
switch(ext){
case "html":
content_type = "text/html";
break;
case "css":
content_type = "text/css";
break;
case "js":
content_type = "text/javascript";
break;
case "ogg":
content_type = "audio/ogg";
break;
case "wav":
content_type = "audio/wav";
break;
case "m4a":
content_type = "audio/aac";
break;
case "mp3":
content_type = "audio/mpeg";
break;
case "png":
content_type = "image/png";
break;
case "jpg":
content_type = "image/jpeg";
break;
case "gif":
content_type = "image/gif";
break;
case "xml":
content_type = "text/xml";
break;
case "json":
content_type = "application/json";
break;
default:
console.log("Unknown file extension!; " + ext + new Date());
res.writeHead(404, {"Content-Type" : "text/plain"});
res.write("Unknown file extension!\n");
res.end();
return;
}
}else{
content_type = "text/html";
}
if(fs.statSync(file_name).isDirectory())
file_name += "/index.html";
fs.readFile(file_name, function(err, data){
if(err){
res.writeHead(500, {"Content-Type" : "text/plain"});
res.write(err + "\n");
res.end();
}else{
res.writeHead(200, {"Content-Type" : content_type});
res.write(data);
res.end();
}
});
}).listen(8080, "localhost");
console.log("Server running at localhost:8080");