-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
38 lines (36 loc) · 1.25 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
var http = require("http");
var url = require("url");
var fs = require("fs");
var server = http.createServer(function (req, res) {
var req_path = url.parse(req.url).pathname;
var filepath = __dirname + req_path;
fs.exists(filepath, function (exists) {
if (exists) {
fs.stat(filepath, function (err, stats) {
if (err) {
res.writeHead(500, { 'Content-Type': 'text/html;charset=utf8' });
res.end('<div styel="color:black;font-size:22px;">server error</div>');
} else {
if (stats.isFile()) {
var file = fs.createReadStream(filepath);
res.writeHead(200, { 'Content-Type': 'text/html;charset=utf8' });
file.pipe(res);
} else {
fs.readdir(filepath, function (err, files) {
var str = '';
for (var i in files) {
str += files[i] + '<br/>';
}
res.writeHead(200, { 'Content-Type': 'text/html;charset=utf8' });
res.write(str);
});
}
}
});
} else {
res.writeHead(404, { 'Content-Type': 'text/html;charset=utf8' });
res.end('<div styel="color:black;font-size:22px;">404 not found</div>');
}
});
});
server.listen('9090', '127.0.0.1');