forked from newghost/websvr
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStaticServer.js
More file actions
150 lines (129 loc) · 3.65 KB
/
Copy pathStaticServer.js
File metadata and controls
150 lines (129 loc) · 3.65 KB
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
141
142
143
144
145
146
147
148
149
150
/*
* Description: Create a static file server (http based).
* This will list all the files and directories via Node.Js.
* The behavior will be like directory browsing enabled in IIS,
* Author: Kris Zhang
* Blog: http://c52u.so
* Required: Node.js: http://www.nodejs.org,
* mime.js: https://github.com/bentomas/node-mime
* Date: 2012-3 Draft
* 2012-4 Update: Using async and mime.js
*/
//StaticServer Namespace
var StaticServer
(function(){
StaticServer = function(strDir, strPort){
var fs = require("fs"),
path = require("path"),
mime = require("./mime"),
//Root path
dir = "C:\\Program Files",
//Listening port
port = 8021,
//How many files?
count = 0;
var urlFormat = function(url){
url = url.replace(/\\/g,'/');
url = url.replace(/ /g,'%20');
return url;
};
//align to right
var date = function(date){
var d = date.getFullYear()
+ '-' + (date.getMonth() + 1)
+ '-' + (date.getDay() + 1)
+ " " + date.toLocaleTimeString();
return " ".substring(0, 20 - d.length) + d;
};
//align to left
var size = function(num){
return num + " ".substring(0, 12 - String(num).length);
};
var anchor = function(txt, url){
url = url ? url : "/";
return '<a href="' + url + '">' + txt + "</a>";
};
var requestHandler = function(request, response){
count = 0;
var url = request.url,
fullPath = path.join(dir, url),
stat;
try{
stat = fs.statSync(fullPath)
}catch(err){
response.writeHead(404, {"Content-Type": "text/html"});
response.end("File not found!");
return;
}
//List all the files in a directory including the all the sub/child folders.
var listFiles = function(callback){
fs.readdir(fullPath, function(err, files){
if(err){
console.log(err);
return;
}
for(var idx = 0, len = files.length; idx < len; idx++){
//persitent the idx before make the sync process
(function(idx){
var filePath = path.join(fullPath, files[idx]),
fileUrl = urlFormat(path.join(url, files[idx]));
fs.stat(filePath, function(err, stat){
if(err){
console.log(err);
return;
}
count++;
response.write(
date(stat.mtime)
+ "\t" + size(stat.size)
+ anchor(files[idx], fileUrl)
+ "\r\n"
);
idx == len -1 && callback();
});
})(idx);
}
});
};
//Is file? Open this file and send to client.
if(stat.isFile()){
fs.readFile(fullPath, function(err, data){
if(err){
console.log(err);
return;
}
response.writeHead(200, {"Content-Type": mime.lookup(fullPath) });
response.end(data, "binary");
});
}
//Is Directory? List all the files and folders.
else if(stat.isDirectory()){
response.writeHead(200, {"Content-Type": "text/html"});
response.write("<h2>http://localhost:" + port + url + "</h2><hr/>");
response.write("<pre>");
response.write(anchor("[To Parent Directory]", url.substr(0, url.lastIndexOf('/'))) + "\r\n\r\n");
listFiles(function(){
response.write("</pre><hr/>");
response.end("<h5>Count: " + count + "</h5>");
});
}
};
// Entry Point
(function(args){
dir = args[2] || strDir || dir;
port = Number(args[3]) || Number(strPort) || port;
try{
//Create http server
require("http").createServer(requestHandler).listen(port);
console.log("Running at localhost"
,"dir:", dir
,"Port:", port
);
}
catch(err){
console.log("Can't setup server at port", port, err);
}
})(process.argv);
};
})();
new StaticServer();