-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4-http-goto-directory.js
47 lines (35 loc) · 1.21 KB
/
4-http-goto-directory.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
const http = require('http');
const url = require('url');
const fs = require('fs');
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((req, res) => {
var list = [];
var path = url.parse(req.url).pathname;
var prev_path = path.split('/').slice(0,-1).join('/');
var baseFolder = '.' + path;
var onDone = function () {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/html');
var back = (path === '/' ? '' : '<a href="' + (prev_path === '' ? '/' : prev_path) + '">..</a><br>');
res.end(back + list.join(''));
}
fs.readdir(baseFolder, (err, files) => {
if (err) { console.error(err); return;}
if (files.length === 0)
return onDone();
for (var i=0; i < files.length; i++) {
fs.stat(baseFolder + '/' + files[i], function (err, stats) {
if (stats.isDirectory())
list.push('<a href="' + (path === '/' ? '' : path) + '/' + this.filename + '">' + this.filename + '</a><br>');
else
list.push(this.filename + '<br>');
if (list.length === this.size)
return onDone();
}.bind({filename: files[i], size: files.length}));
}
})
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});