|
@@ -56,13 +56,17 @@ app.use((req: Request, res: Response, next) => {
|
|
|
// API routes
|
|
// API routes
|
|
|
app.use("/api", apiRoutes);
|
|
app.use("/api", apiRoutes);
|
|
|
|
|
|
|
|
-// 1. 使用一个中间件来同时处理 Angular 的静态文件和路由
|
|
|
|
|
-// 这会为所有以 /app 开头的请求服务静态文件,并确保 Angular 的路由可以工作
|
|
|
|
|
-const angularAppPath = path.join(__dirname, "..", "public", "app");
|
|
|
|
|
-app.use("/app", express.static(angularAppPath));
|
|
|
|
|
|
|
+// 服务 Angular 应用的静态文件
|
|
|
|
|
+// Express 会在 public 目录下寻找请求的静态文件,如 /app/main.js
|
|
|
|
|
+const staticPath = path.join(__dirname, "..", "public");
|
|
|
|
|
+app.use(express.static(staticPath));
|
|
|
|
|
|
|
|
-// 2. 对于所有未被前面路由(如 /api 或 /app)处理的请求,都返回 404
|
|
|
|
|
-// 这是 Express 的默认行为,无需额外代码
|
|
|
|
|
|
|
+// 作为最终的备用,处理所有未被前面路由处理的请求
|
|
|
|
|
+// 这是最标准的单页面应用(SPA)路由处理方式
|
|
|
|
|
+app.use((req: Request, res: Response) => {
|
|
|
|
|
+ const angularAppPath = path.join(__dirname, "..", "public", "app");
|
|
|
|
|
+ res.sendFile(path.join(angularAppPath, "index.html"));
|
|
|
|
|
+});
|
|
|
|
|
|
|
|
// Start the server
|
|
// Start the server
|
|
|
app.listen(port, () => {
|
|
app.listen(port, () => {
|