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

150 lines
4.4 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 verifyNginxFix() {
console.log('🔍 验证 Nginx 修复效果...\n');
console.log('📝 修改说明:');
console.log(' 将 proxy_pass http://localhost:5352/api/government/');
console.log(' 改为 proxy_pass http://localhost:5352');
console.log(' 这样可以保持完整路径,避免路径重复\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
},
{
name: '重复路径接口应该404',
url: 'https://ad.ningmuyun.com/api/government/government/departments',
shouldWork: false
}
];
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(` ✅ 成功: 接口正常工作`);
successCount++;
} else if (response.status === 404) {
console.log(` ❌ 失败: 接口返回 404`);
failCount++;
}
} else {
if (response.status === 404) {
console.log(` ✅ 成功: 正确返回 404重复路径应该不存在`);
successCount++;
} else if (response.status === 200) {
console.log(` ❌ 失败: 重复路径不应该工作`);
failCount++;
}
}
} catch (error) {
if (error.response) {
console.log(` 状态码: ${error.response.status}`);
if (test.shouldWork) {
console.log(` ❌ 失败: ${error.response.statusText}`);
failCount++;
} else {
if (error.response.status === 404) {
console.log(` ✅ 成功: 正确返回 404`);
successCount++;
}
}
} 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');
}
}
// 运行验证
verifyNginxFix().catch(console.error);