weijw 5 months ago
commit
86c96eea5e
6 changed files with 277 additions and 0 deletions
  1. 1 0
      .dockerignore
  2. 1 0
      .gitignore
  3. 18 0
      Dockerfile
  4. 11 0
      app/src/public/index.html
  5. 123 0
      app/src/server.js
  6. 123 0
      src/server.js

+ 1 - 0
.dockerignore

@@ -0,0 +1 @@
+*.tar

+ 1 - 0
.gitignore

@@ -0,0 +1 @@
+*.tar

+ 18 - 0
Dockerfile

@@ -0,0 +1,18 @@
+FROM node:18.20.3-alpine
+
+# 设置工作目录
+WORKDIR /app
+
+# 安装项目依赖
+RUN npm install express --save
+
+# 复制项目文件到工作目录
+COPY ./src /app/src
+
+# 暴露应用监听的端口
+EXPOSE 3000
+
+VOLUME /app/src
+
+# 启动应用
+CMD ["node", "src/server.js"]

+ 11 - 0
app/src/public/index.html

@@ -0,0 +1,11 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+    <meta charset="UTF-8">
+    <meta name="viewport" content="width=device-width, initial-scale=1.0">
+    <title>Document</title>
+</head>
+<body>
+    <h2>hello~~</h2>
+</body>
+</html>

+ 123 - 0
app/src/server.js

@@ -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}`);
+});

+ 123 - 0
src/server.js

@@ -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}`);
+});