-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
140 lines (126 loc) · 4.72 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
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
var arguments = process.argv.splice(2);
var PORT = arguments[0] || 8000;
var http = require('http'), url= require("url"), path= require("path"), fs=require("fs"), os=require('os');
//More media types http://www.iana.org/assignments/media-types/
var mime = {
// text
"css": "text/css",
"js": "text/javascript",
"csv": "text/csv",
"htm": "text/html",
"html": "text/html",
"txt": "text/plain",
"text": "text/plain",
"conf": "text/plain",
"log": "text/plain",
"vtt": "text/vtt",
"wml": "text/vnd.wap.wml",
"wmls": "text/vnd.wap.wmlscript",
"appcache": "text/cache-manifest",
// image
"bmp": "image/bmp",
"gif": "image/gif",
"ico": "image/x-icon",
"jpe": "image/jpeg",
"jpeg": "image/jpeg",
"jpg": "image/jpeg",
"png": "image/png",
"svg": "image/svg+xml",
"webp": "image/webp",
"tiff": "image/tiff",
// application
"json": "application/json",
"xml": "application/xml",
"atom": "application/atom+xml",
"pdf": "application/pdf",
"swf": "application/x-shockwave-flash",
"doc": "application/msword",
"docx": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
"ppt": "application/vnd.ms-powerpoint",
"pptx": "application/vnd.openxmlformats-officedocument.presentationml.presentation",
"xls": "application/vnd.ms-excel",
"xlsx": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
// audio
"wav": "audio/x-wav",
"wma": "audio/x-ms-wma",
"ogg": "audio/ogg",
"m3u": "audio/x-mpegurl",
"weba": "audio/webm",
// video
"mp4": "video/mp4",
"3gp": "video/3gpp",
"mpeg": "video/mpeg",
"f4v": "video/x-f4v",
"wmv": "video/x-ms-wmv",
"avi": "video/x-msvideo"
};
var server = http.createServer(function (request, response) {
var pathname = url.parse(request.url).pathname;
var query = url.parse(request.url).query;
var realPath = "." + pathname;
fs.exists(realPath, function (exists) {
if (!exists) {
if(pathname == '/fetch'){
var arr=query.split('&');
var params={};
for(var i=0; i<arr.length; i++){
var param_arr=arr[i].split('=');
params[param_arr[0]]=param_arr[1];
}
if(params.url){
var chunks = [],
size = 0;
http.get(params.url, function(res) {
res.on("data" , function(chunk){
chunks.push(chunk);
size += chunk.length;
});
res.on("end" , function(){
var data = Buffer.concat(chunks , size);
response.writeHead(200, {'Content-Type': res.headers['content-type']});
response.write(data);
response.end();
});
}).on('error', function(e) {
console.log("Got error: " + e.message);
response.end();
});
}
}else{
response.writeHead(404, {
'Content-Type': 'text/html'
});
response.write('<!DOCTYPE> <html><head> <title>404 Not Found</title> </head><body> <h1>Not Found</h1> <p>The requested URL '+pathname+' was not found on this server.</p> </body></html>');
response.end();
}
} else {
fs.readFile(realPath, "binary", function (err, file) {
if (err) {
response.writeHead(500, {
'Content-Type': 'text/html'
});
response.end('<!DOCTYPE> <html><head> <title>500 Internal Server Error</title> </head><body> <h1>Internal Server Error</h1></body></html>');
} else {
var ext = path.extname(realPath);
ext = ext ? ext.slice(1) : 'unknown';
var contentType = mime[ext] || "text/plain";
response.writeHead(200, {'Content-Type': contentType});
response.write(file, "binary");
response.end();
}
});
}
});
})
server.listen(PORT);
console.log("Server runing at port: " + PORT + ".");
var ifaces=os.networkInterfaces();
for (var dev in ifaces) {
var alias=0;
ifaces[dev].forEach(function(details){
if (details.family=='IPv4') {
console.log(dev+(alias?':'+alias:''), 'http://'+ details.address+":"+PORT);
++alias;
}
});
}