107 lines
3.3 KiB
JavaScript
107 lines
3.3 KiB
JavaScript
// 测试 PostgREST API 连接
|
|
import { setTimeout } from 'node:timers/promises';
|
|
|
|
// 配置信息
|
|
const apiUrl = 'http://nas.7bm.co:54302/api/docauditai';
|
|
const apiToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJyb2xlIjoiYXBpX3VzZXIifQ.KVLm7rZOF0MuX3MR9LqiYA14gba-MaDK_EQXrJ9u5_Y';
|
|
|
|
// 增加超时设置
|
|
const fetchWithTimeout = async (url, options, timeout = 10000) => {
|
|
const controller = new AbortController();
|
|
const timeoutId = setTimeout(() => controller.abort(), timeout);
|
|
|
|
try {
|
|
const response = await fetch(url, {
|
|
...options,
|
|
signal: controller.signal
|
|
});
|
|
clearTimeout(timeoutId);
|
|
return response;
|
|
} catch (error) {
|
|
clearTimeout(timeoutId);
|
|
throw error;
|
|
}
|
|
};
|
|
|
|
async function testPostgREST() {
|
|
try {
|
|
console.log('测试 PostgREST API 连接...');
|
|
console.log(`API URL: ${apiUrl}`);
|
|
|
|
// 1. 测试连接超时问题
|
|
console.log('\n1. 尝试连接 - 基本网络检查');
|
|
try {
|
|
console.log(`正在ping ${new URL(apiUrl).hostname}...`);
|
|
|
|
// 使用简单的fetch检查是否可以连接(即使返回404等错误也表示网络层面是通的)
|
|
const pingResponse = await fetchWithTimeout(apiUrl, {
|
|
method: 'HEAD',
|
|
timeout: 5000
|
|
}, 5000);
|
|
|
|
console.log(`服务器响应:HTTP ${pingResponse.status}`);
|
|
} catch (pingError) {
|
|
console.error('网络测试失败:', pingError.name, pingError.message);
|
|
if (pingError.name === 'AbortError') {
|
|
console.error('连接超时 - 请检查服务器地址和端口是否正确,或者服务器是否在线');
|
|
}
|
|
if (pingError.cause) {
|
|
console.error('错误原因:', pingError.cause);
|
|
}
|
|
|
|
// 不要继续测试
|
|
return;
|
|
}
|
|
|
|
// 2. 测试健康检查端点
|
|
console.log('\n2. 测试健康检查端点');
|
|
try {
|
|
const healthResponse = await fetchWithTimeout(`${apiUrl}/health`, {
|
|
headers: {
|
|
'Authorization': `Bearer ${apiToken}`
|
|
}
|
|
});
|
|
|
|
if (healthResponse.ok) {
|
|
const healthData = await healthResponse.json();
|
|
console.log('健康检查响应:', healthData);
|
|
} else {
|
|
console.log('健康检查失败:', healthResponse.status);
|
|
console.log('响应内容:', await healthResponse.text());
|
|
}
|
|
} catch (healthError) {
|
|
console.error('健康检查请求错误:', healthError);
|
|
}
|
|
|
|
// 3. 尝试获取模式信息
|
|
console.log('\n3. 尝试获取模式信息');
|
|
try {
|
|
const schemaResponse = await fetchWithTimeout(`${apiUrl}/`, {
|
|
headers: {
|
|
'Authorization': `Bearer ${apiToken}`,
|
|
'Accept': 'application/json'
|
|
}
|
|
});
|
|
|
|
if (schemaResponse.ok) {
|
|
const schemaData = await schemaResponse.json();
|
|
console.log('获取到数据库模式信息:',
|
|
Object.keys(schemaData).length > 0 ? Object.keys(schemaData) : schemaData);
|
|
} else {
|
|
console.log('获取模式信息失败:', schemaResponse.status);
|
|
console.log('响应内容:', await schemaResponse.text());
|
|
}
|
|
} catch (schemaError) {
|
|
console.error('获取模式信息错误:', schemaError);
|
|
}
|
|
|
|
} catch (error) {
|
|
console.error('测试过程出现错误:', error);
|
|
if (error.cause) {
|
|
console.error('错误原因:', error.cause);
|
|
}
|
|
}
|
|
}
|
|
|
|
// 执行测试
|
|
testPostgREST();
|