|
@@ -56,17 +56,13 @@ app.use((req: Request, res: Response, next) => {
|
|
|
// API routes
|
|
// API routes
|
|
|
app.use("/api", apiRoutes);
|
|
app.use("/api", apiRoutes);
|
|
|
|
|
|
|
|
-// 1. 服务 Angular 应用的静态文件
|
|
|
|
|
-// 这会处理所有构建产物,例如 .js, .css, .ico 文件
|
|
|
|
|
-const angularAppPath = path.join(__dirname, "..", "public");
|
|
|
|
|
-app.use(express.static(angularAppPath));
|
|
|
|
|
|
|
+// 1. 使用一个中间件来同时处理 Angular 的静态文件和路由
|
|
|
|
|
+// 这会为所有以 /app 开头的请求服务静态文件,并确保 Angular 的路由可以工作
|
|
|
|
|
+const angularAppPath = path.join(__dirname, "..", "public", "app");
|
|
|
|
|
+app.use("/app", express.static(angularAppPath));
|
|
|
|
|
|
|
|
-// 2. 对于所有未被前面路由(如 /api 或静态文件)处理的请求,
|
|
|
|
|
-// 都返回 Angular 应用的 index.html 文件。
|
|
|
|
|
-// app.use() 是一个更通用的中间件,可以作为通配符来处理所有未匹配的请求。
|
|
|
|
|
-app.use((req: Request, res: Response) => {
|
|
|
|
|
- res.sendFile(path.join(angularAppPath, "app", "index.html"));
|
|
|
|
|
-});
|
|
|
|
|
|
|
+// 2. 对于所有未被前面路由(如 /api 或 /app)处理的请求,都返回 404
|
|
|
|
|
+// 这是 Express 的默认行为,无需额外代码
|
|
|
|
|
|
|
|
// Start the server
|
|
// Start the server
|
|
|
app.listen(port, () => {
|
|
app.listen(port, () => {
|