-
Notifications
You must be signed in to change notification settings - Fork 71
/
serve.js
47 lines (36 loc) · 1.15 KB
/
serve.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 express = require('express');
const cors = require('cors');
const fs = require("fs");
const path = require("path");
const app = express();
const publicDir = path.join(__dirname, "public");
app.use(cors());
// Serve static JSON files
app.use(express.static('public'));
// Recursive function to find JSON files
function findJsonFiles(directory, fileList = []) {
const files = fs.readdirSync(directory);
files.forEach((file) => {
const filePath = path.join(directory, file);
const stat = fs.statSync(filePath);
if (stat.isDirectory()) {
findJsonFiles(filePath, fileList);
} else if (path.extname(file) === '.json') {
fileList.push(filePath);
}
});
return fileList;
}
// Endpoint to display links to JSON files
app.get('/', (req, res) => {
const jsonFiles = findJsonFiles(publicDir);
const links = jsonFiles.map((file) => {
const relativePath = path.relative(publicDir, file);
return `<a href="/${relativePath}">${relativePath}</a>`;
});
res.send(links.join('<br>'));
});
// Start the server
app.listen(3001, () => {
console.log('Server is running');
});