|
@@ -0,0 +1,123 @@
|
|
|
+const express = require('express');
|
|
|
+const path = require('path');
|
|
|
+// const multer = require('multer');
|
|
|
+// const { v4: uuidv4 } = require('uuid');
|
|
|
+// const fs = require('fs');
|
|
|
+// const fsp = require('fs/promises');
|
|
|
+
|
|
|
+const app = express();
|
|
|
+
|
|
|
+// // 示例中间件,用于统一响应格式
|
|
|
+// function jsonResponseMiddleware(req, res, next) {
|
|
|
+// // 保存原始的res.json方法
|
|
|
+// res.originalJson = res.json;
|
|
|
+
|
|
|
+// // 重写res.json方法
|
|
|
+// res.json = function (data) {
|
|
|
+// const responseFormat = {
|
|
|
+// code: res.statusCode, // 响应状态码
|
|
|
+// message: res.statusMessage || 'Success', // 状态消息
|
|
|
+// data: data, // 实际数据
|
|
|
+// };
|
|
|
+
|
|
|
+// // 调用原始的res.json方法,发送格式化的响应
|
|
|
+// res.originalJson.call(this, responseFormat);
|
|
|
+// };
|
|
|
+
|
|
|
+// next();
|
|
|
+// }
|
|
|
+
|
|
|
+// // 使用jsonResponseMiddleware中间件
|
|
|
+// app.use(jsonResponseMiddleware);
|
|
|
+
|
|
|
+// 配置静态文件中间件,指定静态文件所在目录
|
|
|
+app.use('/', express.static(path.join(__dirname, 'public')));
|
|
|
+app.use('/api/image', express.static(path.join(__dirname, 'images')));
|
|
|
+app.use('/api/data', express.static(path.join(__dirname, 'data')));
|
|
|
+
|
|
|
+app.get('/api/hello', (req, res) => {
|
|
|
+ res.json({ 'Hello': 'World!' });
|
|
|
+});
|
|
|
+
|
|
|
+// // 设置 Multer 存储引擎
|
|
|
+// const storage = multer.diskStorage({
|
|
|
+// destination: (req, file, cb) => {
|
|
|
+// const fileId = uuidv4().replace(/-/g, '');
|
|
|
+// req.fileId = fileId; // 将 fileId 存储在请求对象中
|
|
|
+// req.fileName = file.originalname;
|
|
|
+
|
|
|
+// const uploadPath = path.join(__dirname, 'upload', fileId);
|
|
|
+// if (!fs.existsSync(uploadPath)) {
|
|
|
+// fs.mkdirSync(uploadPath);
|
|
|
+// }
|
|
|
+// cb(null, uploadPath);
|
|
|
+// },
|
|
|
+// filename: (req, file, cb) => {
|
|
|
+// cb(null, file.originalname);
|
|
|
+// }
|
|
|
+// });
|
|
|
+
|
|
|
+// const upload = multer({ storage: storage });
|
|
|
+
|
|
|
+// app.get('/api/error', (req, res) => {
|
|
|
+// throw 'file not found'
|
|
|
+// });
|
|
|
+
|
|
|
+// // 文件上传接口
|
|
|
+// app.post('/api/file/upload', upload.single('file'), (req, res) => {
|
|
|
+// if (!req.file) {
|
|
|
+// return res.status(400).send('No file uploaded');
|
|
|
+// }
|
|
|
+
|
|
|
+// // 返回文件的 fileId
|
|
|
+// res.json({
|
|
|
+// fileId: req.fileId,
|
|
|
+// fileName: req.fileName,
|
|
|
+// fileSize: req.fileSize,
|
|
|
+// });
|
|
|
+// });
|
|
|
+
|
|
|
+// // 文件下载接口
|
|
|
+// app.get('/api/file/download/:fileId', async (req, res) => {
|
|
|
+// const fileId = req.params.fileId;
|
|
|
+// const dirPath = path.join(__dirname, 'upload', fileId);
|
|
|
+// const files = await fsp.readdir(dirPath);
|
|
|
+// let firstFile = '';
|
|
|
+
|
|
|
+// for (const file of files) {
|
|
|
+// const filePath = path.join(dirPath, file);
|
|
|
+// const stat = await fsp.stat(filePath);
|
|
|
+
|
|
|
+// if (stat.isFile()) {
|
|
|
+// firstFile = filePath;
|
|
|
+// break;
|
|
|
+// }
|
|
|
+// }
|
|
|
+
|
|
|
+// if (firstFile == '') {
|
|
|
+// return res.status(404).send('File not found');
|
|
|
+// }
|
|
|
+
|
|
|
+// // 发送文件
|
|
|
+// res.download(firstFile, (err) => {
|
|
|
+// if (err) {
|
|
|
+// console.error(err);
|
|
|
+// res.status(500).send('Error downloading the file');
|
|
|
+// }
|
|
|
+// });
|
|
|
+// });
|
|
|
+
|
|
|
+// // 错误处理
|
|
|
+// app.use((err, req, res, next) => {
|
|
|
+// res.status(err.status || 500);
|
|
|
+// res.originalJson({
|
|
|
+// code: err.status || 500,
|
|
|
+// message: err.message || err || 'Internal Server Error',
|
|
|
+// });
|
|
|
+// });
|
|
|
+
|
|
|
+// 启动服务器
|
|
|
+const PORT = process.env.PORT || 3000;
|
|
|
+app.listen(PORT, () => {
|
|
|
+ console.log(`Server is running on port ${PORT}`);
|
|
|
+});
|