import * as dotenv from 'dotenv'; import { NestFactory } from '@nestjs/core'; import { ValidationPipe } from '@nestjs/common'; import { Logger } from 'nestjs-pino'; import { AppModule } from './app.module'; import { json } from 'express'; // Load environment variables before anything else dotenv.config(); async function bootstrap() { try { const app = await NestFactory.create(AppModule, { bufferLogs: true }); // Use pino logger for the entire application app.useLogger(app.get(Logger)); const logger = app.get(Logger); logger.log({ message: 'Starting Meteor Backend', service_name: 'meteor-web-backend', env: process.env.NODE_ENV, cwd: process.cwd(), }); // fixme: 打开后json反序列化失败 // // Configure raw body parsing for webhook endpoints // app.use( // '/api/v1/payments/webhook', // json({ // verify: (req: any, res, buf) => { // req.rawBody = buf; // }, // }), // ); app.useGlobalPipes( new ValidationPipe({ whitelist: true, forbidNonWhitelisted: true, transform: true, skipMissingProperties: false, validationError: { target: false }, }), ); // Enable CORS for frontend app.enableCors({ origin: 'http://localhost:3000', // Frontend runs on port 3000 credentials: true, }); const port = process.env.PORT ?? 3000; await app.listen(port); logger.log({ message: 'Application started successfully', service_name: 'meteor-web-backend', port: port, url: `http://localhost:${port}`, }); } catch (error) { // Fallback to console if logger is not available const errorLogger = console; errorLogger.error(JSON.stringify({ timestamp: new Date().toISOString(), level: 'error', service_name: 'meteor-web-backend', message: 'Failed to start application', error: { name: error.name, message: error.message, stack: error.stack, }, })); if ( error.message.includes('database') || error.message.includes('connection') ) { errorLogger.error(JSON.stringify({ timestamp: new Date().toISOString(), level: 'error', service_name: 'meteor-web-backend', message: 'Database connection error detected', troubleshooting: [ 'Database server is running', 'DATABASE_URL in .env is correct', 'Database credentials are valid', 'Network connectivity to database', ], })); } process.exit(1); } } void bootstrap();