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.
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vue2server7",
3
- "version": "7.0.79",
3
+ "version": "7.0.81",
4
4
  "description": "",
5
5
  "scripts": {
6
6
  "dev": "nodemon --watch src --ext ts --exec \"ts-node src/app.ts\"",
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
- app.use(express.static(path.join(__dirname, 'public'))); // 前端页面
9
- app.use('/files', express.static(path.join(__dirname, 'files'))); // 文件下载目录
10
-
11
- // 获取文件列表的API
12
- app.get('/api/files', (req, res) => {
13
- const filesDir = path.join(__dirname, 'files');
14
-
15
- fs.readdir(filesDir, (err, files) => {
16
- if (err) {
17
- return res.status(500).json({ error: '无法读取文件目录' });
18
- }
19
-
20
- // 过滤掉隐藏文件,只返回文件信息
21
- const fileList = files
22
- .filter(file => !file.startsWith('.'))
23
- .map(file => {
24
- const filePath = path.join(filesDir, file);
25
- const stats = fs.statSync(filePath);
26
- return {
27
- name: file,
28
- size: stats.size,
29
- type: path.extname(file).substring(1),
30
- url: `/files/${file}`,
31
- downloadUrl: `/download/${encodeURIComponent(file)}`
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
- app.use((err, req, res, next) => {
83
- conso件目录: ${path.join(__dirname, 'files')}`);
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