|
|
@@ -3,10 +3,10 @@ import dotenv from "dotenv";
|
|
|
dotenv.config(); // 在读取环境变量之前加载 .env 文件
|
|
|
|
|
|
import express, { Request, Response } from "express";
|
|
|
-import { createClient } from "redis"; // 👈 关键修改:从 redis 模块中解构导入 createClient
|
|
|
+import { createClient } from "redis";
|
|
|
import path from "path";
|
|
|
-import apiRoutes from "./routes/apiRoutes"; // Import the new API routes
|
|
|
-import { ClickhouseService } from "./services/clickhouseService"; // 👈 导入 ClickhouseService
|
|
|
+import apiRoutes from "./routes/apiRoutes";
|
|
|
+import { ClickhouseService } from "./services/clickhouseService";
|
|
|
import { connectToDatabase } from "./database";
|
|
|
|
|
|
const app = express();
|
|
|
@@ -54,32 +54,17 @@ app.use((req: Request, res: Response, next) => {
|
|
|
});
|
|
|
|
|
|
// API routes
|
|
|
-// All API routes will be handled by the imported apiRoutes
|
|
|
-app.use("/api", apiRoutes); // Mount the API routes under /api prefix
|
|
|
+app.use("/api", apiRoutes);
|
|
|
|
|
|
-app.use(express.static(path.join(__dirname, "..", "public")));
|
|
|
-// app.use(express.static(path.join(__dirname, "..", "public", "app")));
|
|
|
+// 1. 服务 Angular 应用的静态文件
|
|
|
+// 这会处理所有构建产物,例如 .js, .css, .ico 文件
|
|
|
+const angularAppPath = path.join(__dirname, "..", "public", "app");
|
|
|
+app.use(express.static(angularAppPath));
|
|
|
|
|
|
-app.use("/app", (req: Request, res: Response) => {
|
|
|
- res.sendFile(path.join(__dirname, "../public/app/index.html"));
|
|
|
-});
|
|
|
-
|
|
|
-// 使用正则表达式来匹配所有未被前面路由处理的 GET 请求
|
|
|
-// 这个路由会尝试服务 Angular 应用的 index.html
|
|
|
-app.get(/.*/, (req: Request, res: Response) => {
|
|
|
- // 确保不是 API 请求,才服务 index.html
|
|
|
- // Express 会按顺序处理路由。如果请求是 /api/xxx,会被 app.use('/api', apiRoutes) 捕获。
|
|
|
- // 如果请求是静态文件 (如 /main.js),会被 app.use(express.static) 捕获。
|
|
|
- // 因此,到这里的请求是既非静态文件也非 /api 开头的请求。
|
|
|
- // 为了确保严谨性,可以再次检查路径不以 /api 开头
|
|
|
- if (!req.path.startsWith("/api")) {
|
|
|
- const angularAppPath = path.join(__dirname, "..", "public", "app");
|
|
|
- res.sendFile(path.join(angularAppPath, "index.html"));
|
|
|
- } else {
|
|
|
- // If an API route is not found by apiRoutes, it will fall through here,
|
|
|
- // which means it's an unhandled API route, so return 404.
|
|
|
- res.status(404).json({ message: "API Route not found" });
|
|
|
- }
|
|
|
+// 2. 对于所有未被前面路由(如 /api 或静态文件)处理的请求,
|
|
|
+// 都返回 Angular 应用的 index.html 文件
|
|
|
+app.get("*", (req: Request, res: Response) => {
|
|
|
+ res.sendFile(path.join(angularAppPath, "index.html"));
|
|
|
});
|
|
|
|
|
|
// Start the server
|