luoluoluo22 commited on
Commit
d425192
·
1 Parent(s): f4e4ada

修复: 增强路由处理和添加备用页面

Browse files
Files changed (1) hide show
  1. server.js +118 -3
server.js CHANGED
@@ -26,6 +26,9 @@ import { COLORS } from "./utils/colors.js";
26
  // Load environment variables from .env file
27
  dotenv.config();
28
 
 
 
 
29
  // 打印环境变量(排除敏感信息)
30
  console.log('===== 环境变量 =====');
31
  console.log('NODE_ENV:', process.env.NODE_ENV);
@@ -51,7 +54,12 @@ const MODEL_ID = process.env.DEFAULT_MODEL_ID || "deepseek-ai/DeepSeek-V3-0324";
51
  const MAX_REQUESTS_PER_IP = 4;
52
 
53
  app.use(cookieParser());
54
- app.use(express.static(path.join(__dirname, "dist")));
 
 
 
 
 
55
  app.use(bodyParser.json());
56
 
57
  const getPTag = (repoId) => {
@@ -575,10 +583,117 @@ app.get("/api/remix/:username/:repo", async (req, res) => {
575
  });
576
  });
577
 
578
- app.get("*", (_req, res) => {
579
- res.sendFile(path.join(__dirname, "dist", "index.html"));
 
 
 
 
 
 
 
 
 
 
 
580
  });
581
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
582
  app.listen(PORT, () => {
583
  console.log(`===== 应用启动信息 =====`);
584
  console.log(`启动时间: ${new Date().toISOString()}`);
 
26
  // Load environment variables from .env file
27
  dotenv.config();
28
 
29
+ // 设置环境变量如果不存在
30
+ process.env.APP_PORT = 7860;
31
+
32
  // 打印环境变量(排除敏感信息)
33
  console.log('===== 环境变量 =====');
34
  console.log('NODE_ENV:', process.env.NODE_ENV);
 
54
  const MAX_REQUESTS_PER_IP = 4;
55
 
56
  app.use(cookieParser());
57
+ app.use(express.static(path.join(__dirname, "dist"), {
58
+ index: false, // 不自动提供 index.html,我们会手动处理
59
+ setHeaders: (res, filePath) => {
60
+ console.log(`提供静态文件: ${filePath}`);
61
+ }
62
+ }));
63
  app.use(bodyParser.json());
64
 
65
  const getPTag = (repoId) => {
 
583
  });
584
  });
585
 
586
+ // 添加根路由的专门处理
587
+ app.get("/", (req, res) => {
588
+ console.log("接收到根路径请求");
589
+ console.log("请求头:", req.headers);
590
+
591
+ const indexPath = path.join(__dirname, "dist", "index.html");
592
+ if (fs.existsSync(indexPath)) {
593
+ console.log(`提供 index.html 文件: ${indexPath}`);
594
+ res.sendFile(indexPath);
595
+ } else {
596
+ console.error(`错误: index.html 文件不存在: ${indexPath}`);
597
+ res.status(404).send("找不到主页文件");
598
+ }
599
  });
600
 
601
+ // 修改通配符路由,添加更多日志
602
+ app.get("*", (req, res) => {
603
+ console.log(`通配符路由请求: ${req.url}`);
604
+ const indexPath = path.join(__dirname, "dist", "index.html");
605
+
606
+ if (fs.existsSync(indexPath)) {
607
+ console.log(`通配符路由提供 index.html: ${indexPath}`);
608
+ res.sendFile(indexPath);
609
+ } else {
610
+ console.error(`通配符路由错误: index.html 文件不存在: ${indexPath}`);
611
+ res.status(404).send("找不到页面文件");
612
+ }
613
+ });
614
+
615
+ // 在 app.listen 之前添加
616
+ // 确保 dist 目录和 index.html 存在
617
+ const distDir = path.join(__dirname, "dist");
618
+ const indexPath = path.join(distDir, "index.html");
619
+
620
+ if (!fs.existsSync(distDir)) {
621
+ console.log("创建 dist 目录");
622
+ fs.mkdirSync(distDir, { recursive: true });
623
+ }
624
+
625
+ if (!fs.existsSync(indexPath)) {
626
+ console.log("创建基本的 index.html 文件");
627
+ const basicHtml = `
628
+ <!DOCTYPE html>
629
+ <html lang="zh-CN">
630
+ <head>
631
+ <meta charset="UTF-8">
632
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
633
+ <title>DeepSite</title>
634
+ <style>
635
+ body {
636
+ font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
637
+ display: flex;
638
+ flex-direction: column;
639
+ align-items: center;
640
+ justify-content: center;
641
+ height: 100vh;
642
+ margin: 0;
643
+ padding: 20px;
644
+ text-align: center;
645
+ background-color: #f5f5f5;
646
+ }
647
+ .container {
648
+ max-width: 800px;
649
+ padding: 30px;
650
+ background-color: white;
651
+ box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
652
+ border-radius: 8px;
653
+ }
654
+ h1 {
655
+ color: #0066cc;
656
+ margin-bottom: 20px;
657
+ }
658
+ p {
659
+ font-size: 18px;
660
+ line-height: 1.6;
661
+ color: #333;
662
+ }
663
+ .logo {
664
+ width: 100px;
665
+ height: 100px;
666
+ margin-bottom: 20px;
667
+ }
668
+ .button {
669
+ display: inline-block;
670
+ background-color: #0066cc;
671
+ color: white;
672
+ padding: 12px 24px;
673
+ border-radius: 4px;
674
+ text-decoration: none;
675
+ font-weight: bold;
676
+ margin-top: 20px;
677
+ transition: background-color 0.3s;
678
+ }
679
+ .button:hover {
680
+ background-color: #0052a3;
681
+ }
682
+ </style>
683
+ </head>
684
+ <body>
685
+ <div class="container">
686
+ <h1>欢迎使用 DeepSite</h1>
687
+ <p>一个强大的AI驱动的前端开发助手,帮助你快速创建和优化网页。</p>
688
+ <p>如果您看到此页面,说明应用已成功启动,但可能静态资源加载有问题。</p>
689
+ <a href="/" class="button">刷新页面</a>
690
+ </div>
691
+ </body>
692
+ </html>
693
+ `;
694
+ fs.writeFileSync(indexPath, basicHtml);
695
+ }
696
+
697
  app.listen(PORT, () => {
698
  console.log(`===== 应用启动信息 =====`);
699
  console.log(`启动时间: ${new Date().toISOString()}`);