Files
nxxmdata/government-backend/test-nginx-restart-fix.js
2025-10-17 17:29:11 +08:00

151 lines
4.8 KiB
JavaScript
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.

const axios = require('axios');
// 测试nginx重启后的修复效果
async function testNginxRestartFix() {
console.log('🔍 测试nginx重启后的修复效果...\n');
console.log('📝 修复说明:');
console.log(' 将 proxy_pass http://localhost:5352');
console.log(' 改为 proxy_pass http://localhost:5352/api/government/');
console.log(' 这样可以正确代理到后端API路径\n');
const testCases = [
{
name: '部门列表接口',
url: 'https://ad.ningmuyun.com/api/government/departments',
shouldWork: true
},
{
name: '数据中心接口',
url: 'https://ad.ningmuyun.com/api/government/data-center',
shouldWork: true
},
{
name: '市场价格接口',
url: 'https://ad.ningmuyun.com/api/government/market-price?type=beef',
shouldWork: true
},
{
name: '岗位列表接口',
url: 'https://ad.ningmuyun.com/api/government/positions',
shouldWork: true
},
{
name: '养殖户列表接口',
url: 'https://ad.ningmuyun.com/api/government/farmers',
shouldWork: true
},
{
name: '登录接口',
url: 'https://ad.ningmuyun.com/api/government/auth/login',
method: 'POST',
data: { username: 'admin', password: 'admin123' },
shouldWork: true,
expectedStatus: 401
}
];
let successCount = 0;
let failCount = 0;
for (const test of testCases) {
console.log(`📋 ${test.name}`);
console.log(` URL: ${test.url}`);
try {
const config = {
method: test.method || 'GET',
url: test.url,
headers: {
'Content-Type': 'application/json'
},
timeout: 10000,
validateStatus: function (status) {
return status < 500;
}
};
if (test.data) {
config.data = test.data;
}
const response = await axios(config);
console.log(` 状态码: ${response.status}`);
if (test.shouldWork) {
if (test.expectedStatus) {
if (response.status === test.expectedStatus) {
console.log(` ✅ 成功: 返回预期状态码 ${test.expectedStatus}`);
successCount++;
} else {
console.log(` ❌ 失败: 预期状态码 ${test.expectedStatus},实际 ${response.status}`);
failCount++;
}
} else if (response.status === 200) {
console.log(` ✅ 成功: 接口正常工作`);
if (Array.isArray(response.data)) {
console.log(` 📊 数据量: ${response.data.length} 条记录`);
} else if (response.data && typeof response.data === 'object') {
console.log(` 📊 数据类型: 对象,包含 ${Object.keys(response.data).length} 个字段`);
}
successCount++;
} else if (response.status === 404) {
console.log(` ❌ 失败: 接口返回 404`);
failCount++;
}
}
} catch (error) {
if (error.response) {
console.log(` 状态码: ${error.response.status}`);
if (test.shouldWork) {
console.log(` ❌ 失败: ${error.response.statusText}`);
failCount++;
}
} else {
console.log(` ❌ 网络错误: ${error.message}`);
failCount++;
}
}
console.log(' ' + '─'.repeat(60));
console.log('');
}
console.log('🎯 测试完成!');
console.log(` ✅ 成功: ${successCount}`);
console.log(` ❌ 失败: ${failCount}`);
console.log('');
if (failCount === 0) {
console.log('🎉 所有测试通过Nginx配置修复成功');
} else {
console.log('⚠️ 还有测试失败,请检查:');
console.log(' 1. Nginx配置是否正确修改');
console.log(' 2. Nginx服务是否已重启');
console.log(' 3. 后端服务是否正常运行');
console.log('');
console.log('🔄 重启Nginx:');
console.log(' sudo nginx -t');
console.log(' sudo systemctl reload nginx');
}
console.log('');
console.log('📝 当前nginx配置:');
console.log(' location ^~ /api/government/auth/ {');
console.log(' proxy_pass http://localhost:5352/api/auth/;');
console.log(' }');
console.log(' location ^~ /api/government/ {');
console.log(' proxy_pass http://localhost:5352/api/government/;');
console.log(' }');
console.log('');
console.log('🔄 路径映射:');
console.log(' /api/government/departments → http://localhost:5352/api/government/departments');
console.log(' /api/government/data-center → http://localhost:5352/api/government/data-center');
console.log(' /api/government/market-price → http://localhost:5352/api/government/market-price');
}
// 运行测试
testNginxRestartFix().catch(console.error);