vue2server7 2.3.3 → 2.3.4

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vue2server7",
3
- "version": "2.3.3",
3
+ "version": "2.3.4",
4
4
  "description": "",
5
5
  "scripts": {
6
6
  "dev": "nodemon --watch src --ext ts --exec \"ts-node src/app.ts\"",
package/test/6.js ADDED
@@ -0,0 +1,60 @@
1
+ const express = require('express');
2
+ const fs = require('fs');
3
+ const path = require('path');
4
+
5
+ const app = express();
6
+ const PORT = 3000;
7
+
8
+ // 设置 public 为静态目录
9
+ const publicDir = path.join(__dirname, 'public');
10
+ app.use('/static', express.static(publicDir));
11
+
12
+ // 首页:列出 public 文件夹里的所有文件
13
+ app.get('/', (req, res) => {
14
+ fs.readdir(publicDir, (err, files) => {
15
+ if (err) {
16
+ return res.status(500).send('读取文件夹失败');
17
+ }
18
+
19
+ let fileListHtml = `
20
+ <h1>文件列表</h1>
21
+ <ul>
22
+ `;
23
+
24
+ files.forEach(file => {
25
+ fileListHtml += `
26
+ <li>
27
+ ${file} -
28
+ <a href="/download/${file}">下载</a>
29
+ </li>
30
+ `;
31
+ });
32
+
33
+ fileListHtml += `
34
+ </ul>
35
+ `;
36
+
37
+ res.send(fileListHtml);
38
+ });
39
+ });
40
+
41
+ // 下载接口
42
+ app.get('/download/:filename', (req, res) => {
43
+ const filename = req.params.filename;
44
+ const filePath = path.join(publicDir, filename);
45
+
46
+ // 防止路径穿越攻击
47
+ if (!filePath.startsWith(publicDir)) {
48
+ return res.status(400).send('非法访问');
49
+ }
50
+
51
+ res.download(filePath, filename, (err) => {
52
+ if (err) {
53
+ res.status(404).send('文件不存在');
54
+ }
55
+ });
56
+ });
57
+
58
+ app.listen(PORT, () => {
59
+ console.log(`服务器已启动: http://localhost:${PORT}`);
60
+ });
Binary file