vue2server7 7.0.79 → 7.0.81
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/MybatisX-1.6.3.zip +0 -0
- package/package.json +1 -1
- package/test/1 +32 -77
- package/test/add/file.001 +0 -0
|
Binary file
|
package/package.json
CHANGED
package/test/1
CHANGED
|
@@ -1,84 +1,39 @@
|
|
|
1
1
|
const express = require('express');
|
|
2
2
|
const path = require('path');
|
|
3
|
-
const fs = require('fs');
|
|
3
|
+
const fs = require('fs').promises;
|
|
4
4
|
const app = express();
|
|
5
|
-
const port = process.env.PORT || 3000;
|
|
6
5
|
|
|
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
|
-
res.json(fileList);
|
|
36
|
-
});
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
// 下载文件的路由
|
|
40
|
-
app.get('/download/:filename', (req, res) => {
|
|
41
|
-
const filename = req.params.filename;
|
|
42
|
-
const filePath = path.join(__dirname, 'files', filename);
|
|
43
|
-
|
|
44
|
-
// 检查文件是否存在
|
|
45
|
-
if (!fs.existsSync(filePath)) {
|
|
46
|
-
return res.status(404).send('文件不存在');
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
// 设置响应头,强制下载
|
|
50
|
-
res.download(filePath, (err) => {
|
|
51
|
-
if (err) {
|
|
52
|
-
console.error('下载失败:', err);
|
|
53
|
-
res.status(500).send('下载失败');
|
|
54
|
-
}
|
|
55
|
-
});
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
// 预览文件的路由(可选)
|
|
59
|
-
app.get('/preview/:filename', (req, res) => {
|
|
60
|
-
const filename = req.params.filename;
|
|
61
|
-
const filePath = path.join(__dirname, 'files', filename);
|
|
62
|
-
|
|
63
|
-
if (!fs.existsSync(filePath)) {
|
|
64
|
-
return res.status(404).send('文件不存在');
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
// 对于图片和PDF等可以预览的文件类型
|
|
68
|
-
res.sendFile(filePath);
|
|
69
|
-
});
|
|
70
|
-
|
|
71
|
-
// 默认首页
|
|
72
|
-
app.get('/', (req, res) => {
|
|
73
|
-
res.sendFile(path.join(__dirname, 'public', 'index.html'));
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
// 404 处理
|
|
77
|
-
app.use((req, res) => {
|
|
78
|
-
res.status(404).send('404 - 页面未找到');
|
|
6
|
+
// 静态资源目录(例如 'public')
|
|
7
|
+
const staticDir = path.join(__dirname, 'public');
|
|
8
|
+
|
|
9
|
+
// 1. 使用 express.static 提供静态文件下载能力
|
|
10
|
+
app.use(express.static(staticDir));
|
|
11
|
+
|
|
12
|
+
// 2. 自定义首页路由:列出目录中的文件
|
|
13
|
+
app.get('/', async (req, res) => {
|
|
14
|
+
try {
|
|
15
|
+
const files = await fs.readdir(staticDir);
|
|
16
|
+
const fileLinks = files
|
|
17
|
+
.map(file => `<li><a href="/${encodeURIComponent(file)}" download>${file}</a></li>`)
|
|
18
|
+
.join('');
|
|
19
|
+
|
|
20
|
+
res.send(`
|
|
21
|
+
<html>
|
|
22
|
+
<head><title>文件列表</title></head>
|
|
23
|
+
<body>
|
|
24
|
+
<h2>静态资源目录中的文件:</h2>
|
|
25
|
+
<ul>${fileLinks}</ul>
|
|
26
|
+
</body>
|
|
27
|
+
</html>
|
|
28
|
+
`);
|
|
29
|
+
} catch (err) {
|
|
30
|
+
console.error(err);
|
|
31
|
+
res.status(500).send('无法读取目录');
|
|
32
|
+
}
|
|
79
33
|
});
|
|
80
34
|
|
|
81
|
-
//
|
|
82
|
-
|
|
83
|
-
|
|
35
|
+
// 启动服务器
|
|
36
|
+
const PORT = process.env.PORT || 3000;
|
|
37
|
+
app.listen(PORT, () => {
|
|
38
|
+
console.log(`服务器运行在 http://localhost:${PORT}`);
|
|
84
39
|
});
|
package/test/add/file.001
DELETED
|
Binary file
|