feat: 修改env,使用启动前数据库检查

This commit is contained in:
lichao 2025-07-21 12:31:31 +01:00
parent 8b5d7ed6dc
commit 54486e8074
3 changed files with 80 additions and 32 deletions

4
.env
View File

@ -1,8 +1,8 @@
JWT_SECRET=5270e53a05600cb1f19b287a915b1c792d7552cbaae261afb6dcaf988d0db10f JWT_SECRET=5270e53a05600cb1f19b287a915b1c792d7552cbaae261afb6dcaf988d0db10f
SERVER_HOST='192.144.32.178' SERVER_HOST='192.144.32.178'
SERVER_PORT=3306 SERVER_PORT=3306
SERVER_USER='root' SERVER_USER='golc'
PASSWORD='lichao1314' PASSWORD='Lichao1314!'
DB_NAME='auth_db' DB_NAME='auth_db'
# github # github

23
check-db-schema.js Normal file
View File

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

View File

@ -9,38 +9,63 @@ import * as bodyParser from 'body-parser';
dotenv.config(); dotenv.config();
async function bootstrap() { // 创建数据库的函数
// 启动 Nest 应用 async function createDatabaseIfNotExists() {
const app = await NestFactory.create(AppModule); try {
const configService = app.get(ConfigService); // 获取 ConfigService 实例 // 连接 MySQL不指定数据库
// 连接 MySQL const connection = await mysql.createConnection({
const connection = await mysql.createConnection({ host: process.env.SERVER_HOST,
host: configService.get('SERVER_HOST'), user: process.env.SERVER_USER,
user: configService.get('SERVER_USER'), password: process.env.PASSWORD,
password: configService.get('PASSWORD'), });
});
// 创建数据库(如果不存在) console.log('🔌 连接到 MySQL 服务器成功');
await connection.query('CREATE DATABASE IF NOT EXISTS auth_db');
await connection.end();
app.enableCors({ // 创建数据库(如果不存在)
origin: ['http://108.171.193.155:8000', 'http://localhost:8000'], // 指定允许的前端地址 await connection.query('CREATE DATABASE IF NOT EXISTS auth_db');
credentials: true, // 允许携带凭证 console.log('✅ 数据库 auth_db 创建成功或已存在');
methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS', 'PATCH'], // 允许的 HTTP 方法
allowedHeaders: ['Content-Type', 'Authorization', 'Accept', 'Origin', 'Referer', 'User-Agent'], // 允许的请求头
});
// 注册全局异常过滤器 await connection.end();
app.useGlobalFilters(new HttpExceptionFilter()); console.log('🔌 关闭数据库连接');
} catch (error) {
// 注册全局响应转换拦截器 console.error('❌ 创建数据库失败:', error);
app.useGlobalInterceptors(new TransformInterceptor()); throw error;
}
// 使用 NestJS 内置方法设置限制
app.use(bodyParser.json({ limit: '50mb' }));
app.use(bodyParser.urlencoded({ limit: '50mb', extended: true }));
await app.listen(3030);
} }
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(); bootstrap();