-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
41 lines (36 loc) · 970 Bytes
/
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
// server.js
const express = require('express');
const fs = require('fs');
const path = require('path');
const app = express();
app.use(express.static('.'));
const excludedItems = [
'server.js',
'script.js',
'index.html',
'node_modules',
'.git',
'.gitignore',
'package.json',
'package-lock.json'
];
function getDirectoryContents(dirPath) {
const items = fs.readdirSync(dirPath, { withFileTypes: true });
return items
.filter(item => !excludedItems.includes(item.name))
.map(item => ({
name: item.name,
isDirectory: item.isDirectory(),
path: path.join(dirPath, item.name).replace(/\\/g, '/')
}));
}
app.get('/api/files/*', (req, res) => {
const requestedPath = req.params[0] || '.';
try {
const contents = getDirectoryContents(requestedPath);
res.json(contents);
} catch (err) {
res.status(500).json({ error: err.message });
}
});
app.listen(3000);