oauth_nest_demo/src/main.ts

29 lines
904 B
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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();