-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.mjs
61 lines (48 loc) · 1.58 KB
/
server.mjs
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
import express from 'express';
import path from 'path';
import { fileURLToPath } from 'url';
import fs from 'fs';
import { promisify } from 'util';
const readdir = promisify(fs.readdir);
const stat = promisify(fs.stat);
// 设置 __dirname
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const app = express();
const PORT = process.env.PORT || 3000;
// 提供静态文件
app.use(express.static(path.join(__dirname, 'public')));
// 提供 /srv/comic 目录下的文件
app.use('/srv/comic', express.static('/srv/comic'));
// 路由
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
app.get('/pdf-viewer', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'pdf-viewer.html'));
});
// 处理 /files 请求
app.get('/files', async (req, res) => {
const dirPath = req.query.dir || '/srv/comic';
if (dirPath !== '/srv/comic') {
return res.status(403).send('No Auth');
}
try {
const files = await readdir(dirPath);
const pdfFiles = [];
for (const file of files) {
const filePath = path.join(dirPath, file);
const fileStat = await stat(filePath);
if (fileStat.isFile() && path.extname(file).toLowerCase() === '.pdf') {
pdfFiles.push(file);
}
}
res.json(pdfFiles);
} catch (err) {
console.error(err);
res.status(500).send('Failed to read directory');
}
});
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});