103 lines
3.4 KiB
JavaScript
103 lines
3.4 KiB
JavaScript
// 清除指定模块的缓存
|
||
function clearModuleCache() {
|
||
const modulesToClear = Object.keys(require.cache).filter(key =>
|
||
key.includes('HarmlessPlace') || key.includes('database') || key.includes('controller')
|
||
);
|
||
|
||
console.log('清除以下模块的缓存:', modulesToClear.length, '个模块');
|
||
modulesToClear.forEach(key => {
|
||
console.log('-', key);
|
||
delete require.cache[key];
|
||
});
|
||
}
|
||
|
||
// 模拟服务器启动过程
|
||
async function simulateServerStartup() {
|
||
try {
|
||
// 1. 首先清除模块缓存
|
||
clearModuleCache();
|
||
|
||
// 2. 记录加载顺序
|
||
console.log('\n=== 开始模拟服务器启动过程 ===');
|
||
|
||
// 3. 先加载数据库配置 - 注意这里使用正确的路径
|
||
console.log('\n1. 加载数据库配置...');
|
||
const sequelize = require('./config/database');
|
||
console.log('数据库实例加载完成');
|
||
console.log('sequelize的类型:', typeof sequelize);
|
||
|
||
// 4. 测试数据库连接
|
||
console.log('\n2. 测试数据库连接...');
|
||
try {
|
||
await sequelize.authenticate();
|
||
console.log('数据库连接成功');
|
||
} catch (error) {
|
||
console.error('数据库连接失败:', error.message);
|
||
}
|
||
|
||
// 5. 加载HarmlessPlace模型
|
||
console.log('\n3. 加载HarmlessPlace模型...');
|
||
const HarmlessPlace = require('./models/HarmlessPlace');
|
||
console.log('HarmlessPlace模型加载完成');
|
||
console.log('HarmlessPlace的类型:', typeof HarmlessPlace);
|
||
console.log('HarmlessPlace是否有findAndCountAll方法:', typeof HarmlessPlace.findAndCountAll !== 'undefined');
|
||
if (HarmlessPlace.findAndCountAll) {
|
||
console.log('findAndCountAll的类型:', typeof HarmlessPlace.findAndCountAll);
|
||
}
|
||
|
||
// 6. 尝试调用findAndCountAll方法
|
||
console.log('\n4. 尝试调用findAndCountAll方法...');
|
||
try {
|
||
const result = await HarmlessPlace.findAndCountAll({
|
||
limit: 10,
|
||
offset: 0
|
||
});
|
||
console.log('findAndCountAll调用成功,结果:', result);
|
||
} catch (error) {
|
||
console.error('findAndCountAll调用失败:', error.message);
|
||
}
|
||
|
||
// 7. 加载控制器
|
||
console.log('\n5. 加载HarmlessPlaceController控制器...');
|
||
const harmlessPlaceController = require('./controllers/HarmlessPlaceController');
|
||
console.log('控制器加载完成');
|
||
|
||
// 8. 创建模拟的req和res对象
|
||
const mockReq = {
|
||
query: {
|
||
page: 1,
|
||
pageSize: 10
|
||
}
|
||
};
|
||
|
||
const mockRes = {
|
||
json: function(data) {
|
||
console.log('res.json被调用:', data);
|
||
},
|
||
status: function(code) {
|
||
console.log('res.status被调用:', code);
|
||
return this;
|
||
}
|
||
};
|
||
|
||
// 9. 尝试调用控制器的getList方法
|
||
console.log('\n6. 尝试调用控制器的getList方法...');
|
||
try {
|
||
await harmlessPlaceController.getList(mockReq, mockRes);
|
||
console.log('控制器getList方法调用成功');
|
||
} catch (error) {
|
||
console.error('控制器getList方法调用失败:', error.message);
|
||
console.error('错误堆栈:', error.stack);
|
||
}
|
||
|
||
console.log('\n=== 服务器启动模拟完成 ===');
|
||
|
||
} catch (error) {
|
||
console.error('模拟服务器启动过程中发生错误:', error.message);
|
||
console.error('错误堆栈:', error.stack);
|
||
}
|
||
}
|
||
|
||
// 运行模拟
|
||
console.log('开始执行服务器启动模拟测试...');
|
||
simulateServerStartup().catch(err => console.error('测试过程中出错:', err)); |