-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.js
More file actions
52 lines (41 loc) · 1.3 KB
/
Copy pathtest.js
File metadata and controls
52 lines (41 loc) · 1.3 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
import dotenv from 'dotenv';
import express from 'express';
import fs from 'fs';
import path from 'path';
dotenv.config();
const app = express();
const PORT = process.env.PORT || 3000;
const IGNORED_DIRECTORIES = ['node_modules', '.git'];
function generateFileTree(dirPath, prefix = '') {
let tree = '';
const dirContents = fs.readdirSync(dirPath);
const { length } = dirContents;
dirContents.forEach((itemName, index) => {
// Skip ignored directories
if (IGNORED_DIRECTORIES.includes(itemName)) {
return;
}
const isLast = index === length - 1;
tree += prefix + (isLast ? '└── ' : '├── ') + itemName + '\n';
const itemPath = path.join(dirPath, itemName);
const stats = fs.statSync(itemPath);
if (stats.isDirectory()) {
const newPrefix = prefix + (isLast ? ' ' : '│ ');
tree += generateFileTree(itemPath, newPrefix);
}
});
return tree;
}
app.get('/', (req, res) => {
try {
const tree = generateFileTree('.');
res.type('text/plain');
res.send(tree);
} catch (error) {
res.status(500).send(error.message);
}
});
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
console.log(`http://localhost:${PORT}`)
});