147 lines
4.1 KiB
JavaScript
147 lines
4.1 KiB
JavaScript
|
|
/**
|
|||
|
|
* 启动服务器并测试API
|
|||
|
|
* @file start-and-test.js
|
|||
|
|
* @description 启动服务器并自动测试API接口
|
|||
|
|
*/
|
|||
|
|
|
|||
|
|
const { spawn } = require('child_process');
|
|||
|
|
const axios = require('axios');
|
|||
|
|
|
|||
|
|
let serverProcess = null;
|
|||
|
|
|
|||
|
|
// 启动服务器
|
|||
|
|
function startServer() {
|
|||
|
|
return new Promise((resolve, reject) => {
|
|||
|
|
console.log('🚀 启动服务器...');
|
|||
|
|
|
|||
|
|
serverProcess = spawn('node', ['server.js'], {
|
|||
|
|
stdio: 'pipe',
|
|||
|
|
cwd: __dirname
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
serverProcess.stdout.on('data', (data) => {
|
|||
|
|
const output = data.toString();
|
|||
|
|
console.log(output);
|
|||
|
|
|
|||
|
|
if (output.includes('服务器运行在端口') || output.includes('Server running on port')) {
|
|||
|
|
console.log('✅ 服务器启动成功');
|
|||
|
|
resolve();
|
|||
|
|
}
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
serverProcess.stderr.on('data', (data) => {
|
|||
|
|
console.error('服务器错误:', data.toString());
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
serverProcess.on('error', (error) => {
|
|||
|
|
console.error('启动服务器失败:', error);
|
|||
|
|
reject(error);
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
// 等待5秒让服务器完全启动
|
|||
|
|
setTimeout(() => {
|
|||
|
|
resolve();
|
|||
|
|
}, 5000);
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 测试API
|
|||
|
|
async function testAPI() {
|
|||
|
|
console.log('\n🔍 测试API接口...');
|
|||
|
|
|
|||
|
|
const baseUrl = 'http://localhost:5350';
|
|||
|
|
|
|||
|
|
try {
|
|||
|
|
// 1. 测试根路径
|
|||
|
|
console.log('1. 测试根路径...');
|
|||
|
|
const rootResponse = await axios.get(`${baseUrl}/`);
|
|||
|
|
console.log('✅ 根路径正常:', rootResponse.data.message);
|
|||
|
|
|
|||
|
|
// 2. 测试API文档
|
|||
|
|
console.log('\n2. 测试API文档...');
|
|||
|
|
const docsResponse = await axios.get(`${baseUrl}/api-docs/`);
|
|||
|
|
console.log('✅ API文档页面正常');
|
|||
|
|
|
|||
|
|
// 3. 测试Swagger JSON
|
|||
|
|
console.log('\n3. 测试Swagger JSON...');
|
|||
|
|
const swaggerResponse = await axios.get(`${baseUrl}/api-docs/swagger.json`);
|
|||
|
|
console.log('✅ Swagger JSON正常');
|
|||
|
|
|
|||
|
|
// 4. 检查API路径
|
|||
|
|
console.log('\n4. 检查API路径...');
|
|||
|
|
const paths = Object.keys(swaggerResponse.data.paths || {});
|
|||
|
|
const smartAlertPaths = paths.filter(path => path.includes('/smart-alerts/public'));
|
|||
|
|
|
|||
|
|
console.log(`📋 找到 ${smartAlertPaths.length} 个智能预警API路径:`);
|
|||
|
|
smartAlertPaths.forEach(path => {
|
|||
|
|
console.log(` - ${path}`);
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
if (smartAlertPaths.length > 0) {
|
|||
|
|
console.log('\n🎉 API文档配置成功!');
|
|||
|
|
console.log(`📖 请访问: ${baseUrl}/api-docs`);
|
|||
|
|
} else {
|
|||
|
|
console.log('\n❌ 未找到智能预警API路径');
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 5. 测试实际API调用
|
|||
|
|
console.log('\n5. 测试实际API调用...');
|
|||
|
|
try {
|
|||
|
|
const eartagStatsResponse = await axios.get(`${baseUrl}/api/smart-alerts/public/eartag/stats`);
|
|||
|
|
console.log('✅ 智能耳标预警统计API正常');
|
|||
|
|
} catch (error) {
|
|||
|
|
console.log('⚠️ 智能耳标预警统计API调用失败:', error.message);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
try {
|
|||
|
|
const collarStatsResponse = await axios.get(`${baseUrl}/api/smart-alerts/public/collar/stats`);
|
|||
|
|
console.log('✅ 智能项圈预警统计API正常');
|
|||
|
|
} catch (error) {
|
|||
|
|
console.log('⚠️ 智能项圈预警统计API调用失败:', error.message);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
} catch (error) {
|
|||
|
|
console.error('❌ API测试失败:', error.message);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 停止服务器
|
|||
|
|
function stopServer() {
|
|||
|
|
if (serverProcess) {
|
|||
|
|
console.log('\n🛑 停止服务器...');
|
|||
|
|
serverProcess.kill();
|
|||
|
|
serverProcess = null;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 主函数
|
|||
|
|
async function main() {
|
|||
|
|
try {
|
|||
|
|
await startServer();
|
|||
|
|
await testAPI();
|
|||
|
|
|
|||
|
|
console.log('\n✅ 测试完成!');
|
|||
|
|
console.log('📖 API文档地址: http://localhost:5350/api-docs');
|
|||
|
|
console.log('🔗 基础API地址: http://localhost:5350/api/smart-alerts/public');
|
|||
|
|
console.log('\n按 Ctrl+C 停止服务器');
|
|||
|
|
|
|||
|
|
// 保持服务器运行
|
|||
|
|
process.on('SIGINT', () => {
|
|||
|
|
stopServer();
|
|||
|
|
process.exit(0);
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
} catch (error) {
|
|||
|
|
console.error('❌ 启动失败:', error.message);
|
|||
|
|
stopServer();
|
|||
|
|
process.exit(1);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 如果直接运行此脚本
|
|||
|
|
if (require.main === module) {
|
|||
|
|
main().catch(console.error);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
module.exports = { startServer, testAPI, stopServer };
|