import { NestFactory } from '@nestjs/core'; import { AppModule } from './app.module'; import * as mysql from 'mysql2/promise'; import { ConfigService } from '@nestjs/config'; async function bootstrap() { // 启动 Nest 应用 const app = await NestFactory.create(AppModule); const configService = app.get(ConfigService); // 获取 ConfigService 实例 // 连接 MySQL const connection = await mysql.createConnection({ host: configService.get('SERVER_HOST'), user: configService.get('SERVER_USER'), password: configService.get('PASSWORD'), }); // 创建数据库(如果不存在) await connection.query('CREATE DATABASE IF NOT EXISTS auth_db'); await connection.end(); app.enableCors({ origin: 'http://localhost:8000', // 只允许该来源访问 credentials: true, // 允许带有凭证(cookies)的请求 }); await app.listen(3030); } bootstrap();