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

174 lines
5.6 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 finalNginxTest() {
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(` ✅ 成功: 接口正常工作`);
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++;
}
} 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');
}
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;');
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 # 重新加载配置');
}
// 运行测试
finalNginxTest().catch(console.error);