server.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // 拉迷软装一体化管理系统 — 模式 B 服务端入口
  2. // Express + Parse Server 自建模式
  3. import 'dotenv/config';
  4. import express from 'express';
  5. import cors from 'cors';
  6. import { ParseServer } from 'parse-server';
  7. import Parse from 'parse/node';
  8. import { createRequire } from 'module';
  9. // 加载配置
  10. import configJson from './config.json' with { type: 'json' };
  11. const parseConfig = configJson.parse;
  12. const require = createRequire(import.meta.url);
  13. async function startServer() {
  14. // =============================================
  15. // 1. Express 应用
  16. // =============================================
  17. const app = express();
  18. app.use(cors());
  19. app.use(express.json({ limit: '10mb' }));
  20. app.use(express.urlencoded({ extended: true }));
  21. // =============================================
  22. // 2. Parse Server
  23. // =============================================
  24. const parseServer = new ParseServer({
  25. appId: parseConfig.appId,
  26. masterKey: parseConfig.masterKey,
  27. databaseURI: parseConfig.databaseURI,
  28. serverURL: parseConfig.serverURL,
  29. cloud: undefined, // 手动加载(见下方)
  30. allowClientClassCreation: parseConfig.allowClientClassCreation ?? true,
  31. enableAnonymousUsers: parseConfig.enableAnonymousUsers ?? false,
  32. });
  33. await parseServer.start();
  34. app.use('/parse', parseServer.app);
  35. console.log(`[Parse] Server started at ${parseConfig.serverURL}`);
  36. // =============================================
  37. // 3. Parse SDK 客户端(注入全局,供 api/module 使用)
  38. // =============================================
  39. Parse.initialize(parseConfig.appId);
  40. Parse.serverURL = parseConfig.serverURL;
  41. Parse.masterKey = parseConfig.masterKey;
  42. (globalThis as any).Parse = Parse;
  43. console.log(`[Parse] SDK initialized (appId: ${parseConfig.appId})`);
  44. // =============================================
  45. // 4. Cloud Code(手动加载)
  46. // =============================================
  47. if (parseConfig.cloud) {
  48. try {
  49. require(parseConfig.cloud);
  50. console.log(`[Cloud] Loaded: ${parseConfig.cloud}`);
  51. } catch (e: any) {
  52. console.warn(`[Cloud] Failed to load ${parseConfig.cloud}:`, e.message);
  53. }
  54. }
  55. // =============================================
  56. // 5. 自定义 API 路由
  57. // =============================================
  58. try {
  59. const apiRoutes = await import('./api/routes.js');
  60. app.use('/api', apiRoutes.default);
  61. console.log('[API] Custom routes mounted at /api');
  62. } catch (e: any) {
  63. console.warn('[API] No api/routes.ts found, skipping custom routes:', e.message);
  64. }
  65. // =============================================
  66. // 6. 健康检查
  67. // =============================================
  68. app.get('/api/health', (_, res) => {
  69. res.json({ ok: true, timestamp: new Date().toISOString(), appId: parseConfig.appId });
  70. });
  71. // =============================================
  72. // 7. 启动
  73. // =============================================
  74. const PORT = process.env.PORT || parseConfig.port || 3000;
  75. app.listen(PORT, () => {
  76. console.log(`\n========================================`);
  77. console.log(` 拉迷软装一体化管理系统 API`);
  78. console.log(` http://localhost:${PORT}`);
  79. console.log(` Parse: http://localhost:${PORT}/parse`);
  80. console.log(` Health: http://localhost:${PORT}/api/health`);
  81. console.log(`========================================\n`);
  82. });
  83. }
  84. startServer().catch((err) => {
  85. console.error('Failed to start server:', err);
  86. process.exit(1);
  87. });