luoyutianyang commited on
Commit
51cb1cc
·
verified ·
1 Parent(s): ce35450

Update egg-api.js

Browse files
Files changed (1) hide show
  1. egg-api.js +42 -1
egg-api.js CHANGED
@@ -305,6 +305,47 @@ app.get('/processGif', async (req, res) => {
305
  res.status(500).send('内部服务器错误');
306
  }
307
  });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
308
  app.use(serveStatic(__dirname)); // 添加这一行,设置静态文件服务器
309
  app.get('/', (req, res) => {
310
  res.sendFile(path.join(__dirname, 'index.html')); // 修改这一行,发送HTML文件
@@ -312,4 +353,4 @@ app.get('/', (req, res) => {
312
  // 启动 Express 服务器
313
  app.listen(PORT, () => {
314
  console.log(`服务器运行在端口 ${PORT}`);
315
- });
 
305
  res.status(500).send('内部服务器错误');
306
  }
307
  });
308
+
309
+ const apiStats = {
310
+ '/process-image': 0,
311
+ '/processGif': 0
312
+ };
313
+
314
+ // 中间件记录API请求次数
315
+ app.use((req, res, next) => {
316
+ if (apiStats[req.path] !== undefined) {
317
+ apiStats[req.path]++;
318
+ }
319
+ next();
320
+ });
321
+
322
+ // 提供API请求统计数据的接口
323
+ app.get('/api-stats', (req, res) => {
324
+ res.json(apiStats);
325
+ });
326
+
327
+ // 每天重置统计数据
328
+ setInterval(() => {
329
+ for (const api in apiStats) {
330
+ apiStats[api] = 0;
331
+ }
332
+ }, 24 * 60 * 60 * 1000); // 24小时
333
+
334
+ // 每月重置统计数据
335
+ const resetStatsMonthly = () => {
336
+ const now = new Date();
337
+ const firstDayOfNextMonth = new Date(now.getFullYear(), now.getMonth() + 1, 1);
338
+ const timeUntilNextMonth = firstDayOfNextMonth - now;
339
+ setTimeout(() => {
340
+ for (const api in apiStats) {
341
+ apiStats[api] = 0;
342
+ }
343
+ resetStatsMonthly();
344
+ }, timeUntilNextMonth);
345
+ };
346
+
347
+ resetStatsMonthly();
348
+
349
  app.use(serveStatic(__dirname)); // 添加这一行,设置静态文件服务器
350
  app.get('/', (req, res) => {
351
  res.sendFile(path.join(__dirname, 'index.html')); // 修改这一行,发送HTML文件
 
353
  // 启动 Express 服务器
354
  app.listen(PORT, () => {
355
  console.log(`服务器运行在端口 ${PORT}`);
356
+ });