Spaces:
jiome
/
Running

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

Update index.js

Browse files
Files changed (1) hide show
  1. index.js +31 -0
index.js CHANGED
@@ -1,9 +1,25 @@
1
  const express = require('express');
2
  const fetch = require('node-fetch');
3
  const sharp = require('sharp');
 
 
4
 
5
  const app = express();
6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  app.get('/resize', async (req, res) => {
8
  const imageUrl = req.query.q;
9
 
@@ -42,6 +58,21 @@ app.get('/resize', async (req, res) => {
42
  }
43
  });
44
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
  const PORT = process.env.PORT || 7860;
46
  app.listen(PORT, () => {
47
  console.log(`Server is running on port ${PORT}`);
 
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
+
23
  app.get('/resize', async (req, res) => {
24
  const imageUrl = req.query.q;
25
 
 
58
  }
59
  });
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;
77
  app.listen(PORT, () => {
78
  console.log(`Server is running on port ${PORT}`);