|
@@ -2,7 +2,7 @@
|
|
|
import dotenv from "dotenv";
|
|
import dotenv from "dotenv";
|
|
|
dotenv.config(); // 在读取环境变量之前加载 .env 文件
|
|
dotenv.config(); // 在读取环境变量之前加载 .env 文件
|
|
|
|
|
|
|
|
-import express, { Request, Response } from "express";
|
|
|
|
|
|
|
+import express, { Request, Response, NextFunction } from "express"; // 导入 NextFunction
|
|
|
import { createClient } from "redis";
|
|
import { createClient } from "redis";
|
|
|
import path from "path";
|
|
import path from "path";
|
|
|
import apiRoutes from "./routes/apiRoutes";
|
|
import apiRoutes from "./routes/apiRoutes";
|
|
@@ -48,11 +48,25 @@ redisClient.on("error", (err: any) => console.error("Redis connection error:", e
|
|
|
app.use(express.json());
|
|
app.use(express.json());
|
|
|
|
|
|
|
|
// 新增中间件:为每个API请求添加日志打印
|
|
// 新增中间件:为每个API请求添加日志打印
|
|
|
-app.use((req: Request, res: Response, next) => {
|
|
|
|
|
|
|
+app.use((req: Request, res: Response, next: NextFunction) => {
|
|
|
console.log(`[API Request] ${new Date().toISOString()} - ${req.method} ${req.originalUrl}`);
|
|
console.log(`[API Request] ${new Date().toISOString()} - ${req.method} ${req.originalUrl}`);
|
|
|
next();
|
|
next();
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
|
|
+// --- 在这里添加全局禁用缓存的中间件 ---
|
|
|
|
|
+app.use("/api", (req: Request, res: Response, next: NextFunction) => {
|
|
|
|
|
+ // 为所有 API 路由显式禁用浏览器缓存
|
|
|
|
|
+ res.set({
|
|
|
|
|
+ "Cache-Control": "no-cache, no-store, must-revalidate",
|
|
|
|
|
+ Pragma: "no-cache",
|
|
|
|
|
+ Expires: "0",
|
|
|
|
|
+ ETag: null, // 移除ETag以强制浏览器重新验证
|
|
|
|
|
+ "Last-Modified": null, // 移除Last-Modified以强制浏览器重新验证
|
|
|
|
|
+ });
|
|
|
|
|
+ next();
|
|
|
|
|
+});
|
|
|
|
|
+// ------------------------------------
|
|
|
|
|
+
|
|
|
// API routes
|
|
// API routes
|
|
|
app.use("/api", apiRoutes);
|
|
app.use("/api", apiRoutes);
|
|
|
|
|
|