| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- // 拉迷软装一体化管理系统 — 模式 B 服务端入口
- // Express + Parse Server 自建模式
- import 'dotenv/config';
- import express from 'express';
- import cors from 'cors';
- import { ParseServer } from 'parse-server';
- import Parse from 'parse/node';
- import { createRequire } from 'module';
- // 加载配置
- import configJson from './config.json' with { type: 'json' };
- const parseConfig = configJson.parse;
- const require = createRequire(import.meta.url);
- async function startServer() {
- // =============================================
- // 1. Express 应用
- // =============================================
- const app = express();
- app.use(cors());
- app.use(express.json({ limit: '10mb' }));
- app.use(express.urlencoded({ extended: true }));
- // =============================================
- // 2. Parse Server
- // =============================================
- const parseServer = new ParseServer({
- appId: parseConfig.appId,
- masterKey: parseConfig.masterKey,
- databaseURI: parseConfig.databaseURI,
- serverURL: parseConfig.serverURL,
- cloud: undefined, // 手动加载(见下方)
- allowClientClassCreation: parseConfig.allowClientClassCreation ?? true,
- enableAnonymousUsers: parseConfig.enableAnonymousUsers ?? false,
- });
- await parseServer.start();
- app.use('/parse', parseServer.app);
- console.log(`[Parse] Server started at ${parseConfig.serverURL}`);
- // =============================================
- // 3. Parse SDK 客户端(注入全局,供 api/module 使用)
- // =============================================
- Parse.initialize(parseConfig.appId);
- Parse.serverURL = parseConfig.serverURL;
- Parse.masterKey = parseConfig.masterKey;
- (globalThis as any).Parse = Parse;
- console.log(`[Parse] SDK initialized (appId: ${parseConfig.appId})`);
- // =============================================
- // 4. Cloud Code(手动加载)
- // =============================================
- if (parseConfig.cloud) {
- try {
- require(parseConfig.cloud);
- console.log(`[Cloud] Loaded: ${parseConfig.cloud}`);
- } catch (e: any) {
- console.warn(`[Cloud] Failed to load ${parseConfig.cloud}:`, e.message);
- }
- }
- // =============================================
- // 5. 自定义 API 路由
- // =============================================
- try {
- const apiRoutes = await import('./api/routes.js');
- app.use('/api', apiRoutes.default);
- console.log('[API] Custom routes mounted at /api');
- } catch (e: any) {
- console.warn('[API] No api/routes.ts found, skipping custom routes:', e.message);
- }
- // =============================================
- // 6. 健康检查
- // =============================================
- app.get('/api/health', (_, res) => {
- res.json({ ok: true, timestamp: new Date().toISOString(), appId: parseConfig.appId });
- });
- // =============================================
- // 7. 启动
- // =============================================
- const PORT = process.env.PORT || parseConfig.port || 3000;
- app.listen(PORT, () => {
- console.log(`\n========================================`);
- console.log(` 拉迷软装一体化管理系统 API`);
- console.log(` http://localhost:${PORT}`);
- console.log(` Parse: http://localhost:${PORT}/parse`);
- console.log(` Health: http://localhost:${PORT}/api/health`);
- console.log(`========================================\n`);
- });
- }
- startServer().catch((err) => {
- console.error('Failed to start server:', err);
- process.exit(1);
- });
|