-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
81 lines (72 loc) · 2.67 KB
/
Copy pathserver.js
File metadata and controls
81 lines (72 loc) · 2.67 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
const express = require('express');
const fs = require('fs');
const app = express();
app.use(express.static('public'));
app.set('view engine', 'pug')
let root = "/";
app.use(function(req, res, next) {
let data = [];
let path = __dirname + req.path;
if(fs.lstatSync(path).isFile()) {
console.log('a');
return fs.readFile(path, 'utf8', function (err,data) {
if (err) {
return console.log(err);
}
res.send("<pre>"+data+"</pre>");
});
}
// }
return fs.readdir(path, function(err, items) {
// console.log(path);
// // let root = '';
// if (req.path != '/') {
// // console.log('req.path: ', req.path);
// data.push({name: '..', type:'folder', nextPath: root});
// let reg = /((\/)([\w\-]+)$)/g;
// let substr = req.path.match(reg);
// let length = substr ? substr[0].length : 0;
// console.log(substr);
// console.log(req.path.length);
// console.log(length);
// root = req.path.substring(0, req.path.length - length);
// root = root == '' ? req.path : root;
// console.log("root: ", root);
// // console.log('before: root: ', root);
// // console.log('req.path: ', req.path);
// // if (root.length >= req.path.length) {
// // let reg = /((\/)([\w\-]+)$)/g;
// // let substr = root.match(reg);
// // console.log("substr: ", substr);
// // root = root.substring(0, root.length - substr[0].length);
// // substr = root.match(reg);
// // root = root.substring(0, root.length - substr[0].length);
// // } else {
// // root = req.path;
// // }
// // // root = root.length < req.path.length ? req.path : root;
// // console.log('after:root: ', root);
// }
for (var i=0; i<items.length; i++) {
let info = {};
let reg = /(\.)[(\-)a-zA-Z]+$/gi;
let fileType = items[i].match(reg);
info.name = items[i];
root = req.path;
info.nextPath = req.path == "/" ? info.name : req.path + "/"+ info.name;
if(fileType) {
info.type = info.name[0] == '.' ? 'hidden-file': fileType[0];
} else if (info.name == "LICENSE") {
info.type == "file"
} else {
info.type = "folder";
}
data.push(info);
}
res.render(__dirname + '/views/index', {data: data, root: root});
});
});
// listen for requests :)
const listener = app.listen(process.env.PORT, function() {
console.log('Your app is listening on port ' + listener.address().port);
});