test-api-config.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // API配置测试脚本
  2. import http from 'http';
  3. console.log('🧪 测试API配置功能...\n');
  4. async function testApiConnection() {
  5. console.log('1. 测试后端API连接...');
  6. return new Promise((resolve, reject) => {
  7. const req = http.request({
  8. hostname: 'localhost',
  9. port: 8000,
  10. path: '/api/announcement?page=1&page_size=10',
  11. method: 'GET',
  12. headers: {
  13. 'Content-Type': 'application/json'
  14. }
  15. }, (res) => {
  16. let data = '';
  17. console.log(`状态码: ${res.statusCode}`);
  18. res.on('data', (chunk) => {
  19. data += chunk;
  20. });
  21. res.on('end', () => {
  22. try {
  23. const response = JSON.parse(data);
  24. console.log('API响应:', JSON.stringify(response, null, 2));
  25. resolve(response);
  26. } catch (e) {
  27. console.error('解析响应失败:', e);
  28. reject(e);
  29. }
  30. });
  31. });
  32. req.on('error', (e) => {
  33. console.error('请求失败:', e);
  34. reject(e);
  35. });
  36. req.end();
  37. });
  38. }
  39. // 运行测试
  40. testApiConnection()
  41. .then(() => {
  42. console.log('\n✅ API连接测试成功!');
  43. })
  44. .catch((error) => {
  45. console.error('\n❌ API连接测试失败:', error.message);
  46. });