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

161 lines
5.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');
// 测试location规则顺序修复效果
async function testLocationOrderFix() {
console.log('🔍 测试location规则顺序修复效果...\n');
console.log('📝 修复说明:');
console.log(' 调整了location规则的顺序让API规则优先于静态文件规则');
console.log(' 1. location ^~ /api/government/auth/ (最高优先级)');
console.log(' 2. location ^~ /api/government/ (第二优先级)');
console.log(' 3. location ^~ /government/ (静态文件,最低优先级)');
console.log('');
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('🎉 所有测试通过Location规则顺序修复成功');
} 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(' 1. location ^~ /api/government/auth/ {');
console.log(' proxy_pass http://localhost:5352/api/auth/;');
console.log(' }');
console.log(' 2. location ^~ /api/government/ {');
console.log(' proxy_pass http://localhost:5352/api/government/;');
console.log(' }');
console.log(' 3. location ^~ /government/ {');
console.log(' alias /data/vue/ningmuyun/government/dist/;');
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');
console.log('');
console.log('🚨 重要提醒:');
console.log(' 修改nginx配置后必须重启nginx服务才能生效');
console.log(' sudo nginx -t # 检查配置语法');
console.log(' sudo systemctl reload nginx # 重新加载配置');
}
// 运行测试
testLocationOrderFix().catch(console.error);