-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathserver.js
executable file
·138 lines (126 loc) · 4.22 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
/*
$ node server.js
or
$ node server.js 1234
*/
var http = require("http"),
url = require("url"),
path = require("path"),
fs = require("fs")
port = process.argv[2] || 80;
function formidable(req, res) {
// parse a file upload
var expect = 'sp';
var sp, cont, type, total = 0;
req.on('data', function(tr) {
//console.log('trunk:', tr, tr.toString());
while(1) {
switch(expect) {
case 'sp':
var idx = tr.indexOf('\r\n');
sp = tr.slice(0, idx).toString();
tr = tr.slice(idx+2);
console.log(sp);
expect = 'content';
break;
case 'content':
var idx = tr.indexOf('\r\n');
cont = tr.slice(0, idx).toString();
console.log(cont);
if(/Content-Disposition: ?form-data;.*filename="/.test(cont)) {
expect = 'type';
tr = tr.slice(idx+2);
} else {
expect = 'value';
tr = tr.slice(idx+4);
}
break;
case 'value':
var idx = tr.indexOf('\r\n');
value = tr.slice(0, idx).toString();
tr = tr.slice(idx+2);
console.log(value);
expect = 'sp';
break;
case 'type':
var idx = tr.indexOf('\r\n');
type = tr.slice(0, idx).toString();
tr = tr.slice(idx+4);
console.log(type);
expect = 'end';
break;
case 'end':
var idx = tr.indexOf('\r\n'+sp);
console.log('data length:', tr.length);
if(idx >= 0) {
total += idx;
} else total += tr.length;
return;
}
}
}).on('end',function() {
console.log('total:', total);
//res.statusCode = 404;
//res.end('not fonddound');
res.end(`<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk" />
</head>
<body><p>${cont}</p>
<p>total: ${total}</p>
</body>`);
});
}
var mimeTypes = {
"htm": "text/html",
"html": "text/html",
"jpeg": "image/jpeg",
"jpg": "image/jpeg",
"png": "image/png",
"gif": "image/gif",
"js": "text/javascript",
"css": "text/css"};
var virtualDirectories = {
//"images": "../images/"
};
process.chdir(__dirname);
http.createServer(function(request, response) {
if (request.url == '/upload' && request.method.toLowerCase() == 'post') {
console.log('upload', request.url);
formidable(request, response);
return;
}
var uri = url.parse(request.url).pathname
, filename = path.join(process.cwd(), uri)
, root = uri.split("/")[1]
, virtualDirectory;
virtualDirectory = virtualDirectories[root];
if(virtualDirectory){
uri = uri.slice(root.length + 1, uri.length);
filename = path.join(virtualDirectory ,uri);
}
fs.exists(filename, function(exists) {
if(!exists) {
response.writeHead(404, {"Content-Type": "text/plain"});
response.write("404 Not Found\n");
response.end();
console.error('404: ' + filename);
return;
}
if (fs.statSync(filename).isDirectory()) filename += '/index.html';
fs.readFile(filename, "binary", function(err, file) {
if(err) {
response.writeHead(500, {"Content-Type": "text/plain"});
response.write(err + "\n");
response.end();
console.error('500: ' + filename);
return;
}
var mimeType = mimeTypes[path.extname(filename).split(".")[1]];
response.writeHead(200, {"Content-Type": mimeType});
response.write(file, "binary");
response.end();
console.log('200: ' + filename + ' as ' + mimeType);
});
});
}).listen(parseInt(port, 10));
console.log("Static file server running at\n => http://localhost:" + port + "/\nCTRL + C to shutdown");