index.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /**
  2. * 总启动器
  3. *
  4. * 职责(遵循《后端目录规范》第八节):
  5. * - 加载环境变量(dotenv.config())
  6. * - 启动 PC 端服务(单端口,所有模块路由统一挂载)
  7. * - 启动 Mobile 端服务(单端口)
  8. * - 处理进程级事件
  9. *
  10. * 端口规划:
  11. * 3101 — PC 端(11 个业务模块共用一个 Express App)
  12. * 3201 — Mobile 端(chat 等模块)
  13. *
  14. * 前端只需配置两个 baseURL:
  15. * PC_BASE_URL = http://localhost:3101/api
  16. * MOBILE_BASE_URL = http://localhost:3201/api
  17. */
  18. import { config } from 'dotenv';
  19. import { startPcServer } from './apps/pc/health/server.js';
  20. import { startMobileChatServer } from './apps/mobile/chat/server.js';
  21. // 必须在读取任何 process.env 之前调用
  22. config();
  23. console.log('===========================================');
  24. console.log(' Lami Base — 后端服务启动中...');
  25. console.log('===========================================');
  26. // PC 端(3101)—— 所有模块共用一个端口
  27. startPcServer();
  28. // Mobile 端(3201)—— AI 聊天等
  29. startMobileChatServer();
  30. // ---- 进程事件 ----
  31. process.on('SIGINT', () => {
  32. console.log('\n收到 SIGINT,正在退出...');
  33. process.exit(0);
  34. });
  35. process.on('SIGTERM', () => {
  36. console.log('\n收到 SIGTERM,正在退出...');
  37. process.exit(0);
  38. });
  39. process.on('uncaughtException', (error) => {
  40. console.error('未捕获的异常:', error);
  41. process.exit(1);
  42. });
  43. process.on('unhandledRejection', (reason) => {
  44. console.error('未处理的 Promise 拒绝:', reason);
  45. });