nagose commited on
Commit
0740a8c
·
verified ·
1 Parent(s): 05c66dd

Update index.js

Browse files
Files changed (1) hide show
  1. index.js +11 -22
index.js CHANGED
@@ -1,22 +1,19 @@
1
  const express = require('express');
2
  const fetch = require('node-fetch');
3
  const sharp = require('sharp');
4
- const fs = require('fs');
5
- const path = require('path');
6
 
7
  const app = express();
8
 
9
- // 日志文件路径
10
- const logFilePath = path.join(__dirname, 'server.log');
11
 
12
- // 中间件:记录日志到文件
13
  app.use((req, res, next) => {
14
- const logEntry = `${new Date().toISOString()} - ${req.method} ${req.url}\n`;
15
- fs.appendFile(logFilePath, logEntry, (err) => {
16
- if (err) {
17
- console.error('写入日志文件时出错:', err);
18
- }
19
- });
20
  next();
21
  });
22
 
@@ -60,17 +57,9 @@ app.get('/resize', async (req, res) => {
60
 
61
  // 新增的 /log 路由
62
  app.get('/log', (req, res) => {
63
- fs.readFile(logFilePath, 'utf8', (err, data) => {
64
- if (err) {
65
- console.error('读取日志文件时出错:', err);
66
- return res.status(500).send('读取日志文件时出错');
67
- }
68
-
69
- const logs = data.trim().split('\n');
70
- const recentLogs = logs.slice(-100).join('\n');
71
- res.setHeader('Content-Type', 'text/plain');
72
- res.send(recentLogs);
73
- });
74
  });
75
 
76
  const PORT = process.env.PORT || 7860;
 
1
  const express = require('express');
2
  const fetch = require('node-fetch');
3
  const sharp = require('sharp');
 
 
4
 
5
  const app = express();
6
 
7
+ // 全局变量:存储最近的日志信息
8
+ const logs = [];
9
 
10
+ // 中间件:记录日志到全局变量
11
  app.use((req, res, next) => {
12
+ const logEntry = `${new Date().toISOString()} - ${req.method} ${req.url}`;
13
+ logs.push(logEntry);
14
+ if (logs.length > 100) {
15
+ logs.shift(); // 保持日志数量不超过100条
16
+ }
 
17
  next();
18
  });
19
 
 
57
 
58
  // 新增的 /log 路由
59
  app.get('/log', (req, res) => {
60
+ const recentLogs = logs.join('\n');
61
+ res.setHeader('Content-Type', 'text/plain');
62
+ res.send(recentLogs);
 
 
 
 
 
 
 
 
63
  });
64
 
65
  const PORT = process.env.PORT || 7860;