|
@@ -56,15 +56,27 @@ app.use((req: Request, res: Response, next) => {
|
|
|
// API routes
|
|
// API routes
|
|
|
app.use("/api", apiRoutes);
|
|
app.use("/api", apiRoutes);
|
|
|
|
|
|
|
|
|
|
+// 动态设置公共目录和Angular应用路径
|
|
|
|
|
+let publicPath: string;
|
|
|
|
|
+let angularAppPath: string;
|
|
|
|
|
+
|
|
|
|
|
+if (process.env.NODE_ENV === "production") {
|
|
|
|
|
+ // 如果在生产环境中,从 dist 目录往上一层找到 public
|
|
|
|
|
+ publicPath = path.resolve(__dirname, "..", "..", "public");
|
|
|
|
|
+ angularAppPath = path.join(publicPath, "app");
|
|
|
|
|
+} else {
|
|
|
|
|
+ // 如果在开发环境中,public 目录就在当前目录的上一层(相对于src)
|
|
|
|
|
+ publicPath = path.resolve(__dirname, "..", "public");
|
|
|
|
|
+ angularAppPath = path.join(publicPath, "app");
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
// 服务 Angular 应用的静态文件
|
|
// 服务 Angular 应用的静态文件
|
|
|
// Express 会在 public 目录下寻找请求的静态文件,如 /app/main.js
|
|
// Express 会在 public 目录下寻找请求的静态文件,如 /app/main.js
|
|
|
-const staticPath = path.join(__dirname, "..", "public");
|
|
|
|
|
-app.use(express.static(staticPath));
|
|
|
|
|
|
|
+app.use(express.static(publicPath));
|
|
|
|
|
|
|
|
// 作为最终的备用,处理所有未被前面路由处理的请求
|
|
// 作为最终的备用,处理所有未被前面路由处理的请求
|
|
|
// 这是最标准的单页面应用(SPA)路由处理方式
|
|
// 这是最标准的单页面应用(SPA)路由处理方式
|
|
|
app.use((req: Request, res: Response) => {
|
|
app.use((req: Request, res: Response) => {
|
|
|
- const angularAppPath = path.join(__dirname, "..", "public", "app");
|
|
|
|
|
res.sendFile(path.join(angularAppPath, "index.html"));
|
|
res.sendFile(path.join(angularAppPath, "index.html"));
|
|
|
});
|
|
});
|
|
|
|
|
|