-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
41 lines (37 loc) · 1.15 KB
/
index.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
const express = require('express');
const cors = require('cors');
const fs = require('fs');
const path = require('path');
const app = express();
const PORT = 5409;
app.use(cors())
app.get('/', (req, res) => {
res.redirect('/fonts');
})
app.use((req, res, next) => {
const filePath = path.join("./", req.path);
if (!fs.existsSync(filePath)) {
return res.status(404).send("File not found");
}
const stats = fs.statSync(filePath);
if (stats.isDirectory()) {
let files = fs.readdirSync(filePath);
if (files.length === 0) {
return res.status(404).send("File not found");
}
let returnStr = '';
for (let i = 0; i < files.length; i++) {
let fullLink = path.join(req.path, files[i]);
returnStr += `<a href="${fullLink}">${files[i]}</a><br>`;
}
return res.send(`<a href="../">../</a><br>` + returnStr);
}
if (req.query.download === 'true') {
res.setHeader('Content-Disposition', 'attachment');
}
next();
});
app.use('/fonts', express.static('fonts'));
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});