You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用axios请求Chess.com存档URL时遭遇502状态码错误

Node.js请求Chess.com API遇502错误及代码语法修复

问题说明

我编写了Node.js代码,通过axios向Chess.com玩家游戏存档URL发送GET请求,预期返回包含游戏字典的数组。代码会遍历archiveUrls中的多个URL,用winston日志库记录响应数据和错误,但实际运行时触发Request failed with status code 502错误。此外,代码中fetchArchive函数存在重复async声明的语法问题,需先修正该问题再排查502错误。

原代码

/**
 * Makes GET requests to the given archive URLs, logs the response data, and logs any errors that occur.
 * 
 * This program makes GET requests to the archive URLs in the `archiveUrls` array. The response data is ,
 * logged and any errors that occur during the requests or while logging the response data are logged 
 * as well. The errors are logged using the winston library's error level.
 */

const axios = require('axios');
const winston = require('winston');

/**
 * Creates a logger for logging errors.
 *
 * @constant logger
 */
const logger = winston.createLogger({
  level: 'error',
  format: winston.format.json(),
  transports: [
    new winston.transports.Console({ format: winston.format.simple() })
  ]
});

/**
 * Makes a GET request to the given `archiveUrl` and returns the response data.
 * If the request fails, logs an error and throws an error.
 *
 * @function fetchArchive
 * @async
 * 
 * @param {string} archiveUrl - The URL of the archive to fetch.
 * 
 * @throws {Error} If the request fails.
 * 
 * @returns {object} The response data.
 */

async 
async function fetchArchive(archiveUrl) {
  try {
    const response = await axios.get(archiveUrl);
    return response.data;
  } catch (error) {
    logger.error(`Error while fetching archive: ${error}`);
    throw error;
  }
}

/**
 * Makes GET requests to the given archive URLs, logs the response data, and logs any errors that occur.
 *
 * @function main
 * @async
 * 
 * @throws {Error} If there is an error parsing the JSON.
 */

async function main() {
  try {
    const archiveUrls = [
      'https://api.chess.com/pub/player/stolelaf88/games/2021/03',
      'https://api.chess.com/pub/player/puchov/games/2014/03',
      'https://api.chess.com/pub/player/tennessi/games/2020/08'
    ];
    // Note: archiveUrls can be found at the value fields of the https://api.chess.com/pub/player/${username}/games/archives with key "archives."

    for (const archiveUrl of archiveUrls) {
      console.log(`Fetching archive from URL: ${archiveUrl}`);
      const archive = await fetchArchive(archiveUrl);
      console.log(archive);
    }
  } catch (error) {
    logger.error(`Error parsing JSON: ${error}`);
  } 
}

main();

预期结果

[
  {
      "url": "string", // URL of this game,
      "pgn": "string", // final PGN,
      "time_control": "string", // PGN-compliant time control,
      "end_time": 1254670734, // timestamp of the game end,
      "rated": true, //whether the game is rated or not,
      "fen": "string", // final FEN,
      "time_class": "string", // chess.com time control category,
      "white": {}, // details of the white-piece player,
      "black": {} // details of the black-piece player
    }
]

错误信息

error: Error while fetching archive: AxiosError: Request failed with status code 502
error: Error parsing JSON for username stolelaf88: AxiosError: Request failed with status code 502
error: Error parsing JSON for username stolelaf88: AxiosError: Request failed with status code 502

解决步骤

1. 修复语法错误

原代码中fetchArchive函数前有一行单独的async声明,这是语法错误,直接删除该行即可:

修正后的fetchArchive函数:

async function fetchArchive(archiveUrl) {
  try {
    const response = await axios.get(archiveUrl);
    return response.data;
  } catch (error) {
    logger.error(`Error while fetching archive: ${error}`);
    throw error;
  }
}

2. 排查并解决502错误

502 Bad Gateway通常表示Chess.com的API网关返回了无效响应,可能的原因和解决办法如下:

  • 验证URL有效性:手动访问archiveUrls中的链接,确认是否存在玩家或该月份是否有游戏记录。部分URL可能因玩家不存在、账号删除或对应月份无游戏而返回错误。
  • 添加请求重试机制:502可能是临时故障,通过重试可解决。可以使用axios-retry库实现自动重试。
  • 遵守API速率限制:Chess.com API限制每分钟最多100次请求,在请求间添加延迟避免触发限制。
  • 设置User-Agent:Chess.com API要求请求携带标识来源的User-Agent,避免被拦截。
  • 捕获详细错误信息:在catch块中记录响应状态码、响应头和错误详情,帮助定位问题。

修正后的完整代码(含重试、User-Agent和延迟)

const axios = require('axios');
const winston = require('winston');
const axiosRetry = require('axios-retry');

// 配置axios重试
axiosRetry(axios, {
  retries: 3,
  retryDelay: (retryCount) => {
    return retryCount * 1000; // 每次重试间隔1秒递增
  },
  retryCondition: (error) => {
    // 仅重试502、503、504等服务器错误
    return error.response?.status >= 500;
  }
});

const logger = winston.createLogger({
  level: 'error',
  format: winston.format.json(),
  transports: [
    new winston.transports.Console({ format: winston.format.simple() })
  ]
});

async function fetchArchive(archiveUrl) {
  try {
    const response = await axios.get(archiveUrl, {
      headers: {
        'User-Agent': 'MyChessApp/1.0 (your-email@example.com)' // 替换为你的应用标识和邮箱
      }
    });
    return response.data;
  } catch (error) {
    // 记录详细错误信息
    logger.error(`
      Error fetching ${archiveUrl}:
      Status: ${error.response?.status}
      Message: ${error.message}
      Response: ${JSON.stringify(error.response?.data || 'No response data')}
    `);
    throw error;
  }
}

async function delay(ms) {
  return new Promise(resolve => setTimeout(resolve, ms));
}

async function main() {
  try {
    const archiveUrls = [
      'https://api.chess.com/pub/player/stolelaf88/games/2021/03',
      'https://api.chess.com/pub/player/puchov/games/2014/03',
      'https://api.chess.com/pub/player/tennessi/games/2020/08'
    ];

    for (const archiveUrl of archiveUrls) {
      console.log(`Fetching archive from URL: ${archiveUrl}`);
      const archive = await fetchArchive(archiveUrl);
      console.log(archive);
      await delay(1000); // 请求间隔1秒,避免触发速率限制
    }
  } catch (error) {
    logger.error(`Main process error: ${error}`);
  } 
}

main();

内容的提问来源于stack exchange,提问作者Iftikhar Ramnandan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 07:15:33