diff --git a/.env b/.env index d920c9f..53bacce 100644 --- a/.env +++ b/.env @@ -1,8 +1,8 @@ JWT_SECRET=5270e53a05600cb1f19b287a915b1c792d7552cbaae261afb6dcaf988d0db10f SERVER_HOST='192.144.32.178' SERVER_PORT=3306 -SERVER_USER='root' -PASSWORD='lichao1314' +SERVER_USER='golc' +PASSWORD='Lichao1314!' DB_NAME='auth_db' # github diff --git a/check-db-schema.js b/check-db-schema.js new file mode 100644 index 0000000..d354203 --- /dev/null +++ b/check-db-schema.js @@ -0,0 +1,23 @@ +const mysql = require('mysql2/promise'); + +async function checkSchema() { + try { + const connection = await mysql.createConnection({ + host: '192.144.32.178', + port: 3306, + user: 'root', + password: 'lichao1314', + database: 'auth_db' + }); + + console.log('🔍 检查当前 user 表结构:'); + const [rows] = await connection.execute('DESCRIBE user'); + console.table(rows); + + await connection.end(); + } catch (error) { + console.error('❌ 数据库连接错误:', error.message); + } +} + +checkSchema(); diff --git a/src/main.ts b/src/main.ts index 13433c0..d304b85 100644 --- a/src/main.ts +++ b/src/main.ts @@ -9,38 +9,63 @@ import * as bodyParser from 'body-parser'; dotenv.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'), - }); +// 创建数据库的函数 +async function createDatabaseIfNotExists() { + try { + // 连接 MySQL(不指定数据库) + const connection = await mysql.createConnection({ + host: process.env.SERVER_HOST, + user: process.env.SERVER_USER, + password: process.env.PASSWORD, + }); - // 创建数据库(如果不存在) - await connection.query('CREATE DATABASE IF NOT EXISTS auth_db'); - await connection.end(); + console.log('🔌 连接到 MySQL 服务器成功'); - app.enableCors({ - origin: ['http://108.171.193.155:8000', 'http://localhost:8000'], // 指定允许的前端地址 - credentials: true, // 允许携带凭证 - methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS', 'PATCH'], // 允许的 HTTP 方法 - allowedHeaders: ['Content-Type', 'Authorization', 'Accept', 'Origin', 'Referer', 'User-Agent'], // 允许的请求头 - }); + // 创建数据库(如果不存在) + await connection.query('CREATE DATABASE IF NOT EXISTS auth_db'); + console.log('✅ 数据库 auth_db 创建成功或已存在'); - // 注册全局异常过滤器 - app.useGlobalFilters(new HttpExceptionFilter()); - - // 注册全局响应转换拦截器 - app.useGlobalInterceptors(new TransformInterceptor()); - - // 使用 NestJS 内置方法设置限制 - app.use(bodyParser.json({ limit: '50mb' })); - app.use(bodyParser.urlencoded({ limit: '50mb', extended: true })); - - await app.listen(3030); + await connection.end(); + console.log('🔌 关闭数据库连接'); + } catch (error) { + console.error('❌ 创建数据库失败:', error); + throw error; + } } + +async function bootstrap() { + try { + // 1. 首先创建数据库 + await createDatabaseIfNotExists(); + + // 2. 然后启动 Nest 应用(此时数据库已存在) + console.log('🚀 启动 NestJS 应用...'); + const app = await NestFactory.create(AppModule); + const configService = app.get(ConfigService); // 获取 ConfigService 实例 + + app.enableCors({ + origin: ['http://108.171.193.155:8000', 'http://localhost:8000'], // 指定允许的前端地址 + credentials: true, // 允许携带凭证 + methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS', 'PATCH'], // 允许的 HTTP 方法 + allowedHeaders: ['Content-Type', 'Authorization', 'Accept', 'Origin', 'Referer', 'User-Agent'], // 允许的请求头 + }); + + // 注册全局异常过滤器 + app.useGlobalFilters(new HttpExceptionFilter()); + + // 注册全局响应转换拦截器 + app.useGlobalInterceptors(new TransformInterceptor()); + + // 使用 NestJS 内置方法设置限制 + app.use(bodyParser.json({ limit: '50mb' })); + app.use(bodyParser.urlencoded({ limit: '50mb', extended: true })); + + await app.listen(3030); + console.log('✅ 应用启动成功,监听端口 3030'); + } catch (error) { + console.error('❌ 应用启动失败:', error); + process.exit(1); + } +} + bootstrap();