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

Node.js HTTPS请求调用YouTube图片API超时问题求助

解决Node.js请求YouTube缩略图时的ETIMEDOUT问题

问题原因

错误信息里的10.10.34.35是私有IP,说明你的请求被导向了内部代理服务器,但当前Node.js代码没有配置代理参数,导致连接超时。浏览器和Postman会自动读取系统代理配置,所以能正常访问,而Node.js的https.get默认不会自动使用代理。

解决方案

方案1:给https.Agent添加代理配置

修改代码,在Agent中加入代理地址、端口(如果需要认证还要加账号密码):

const https = require('https');

// 替换为你的实际代理信息
const proxyConfig = {
  host: '你的代理IP',
  port: 你的代理端口,
  // 代理需要认证时添加:auth: '用户名:密码'
};

const agent = new https.Agent({
  keepAlive: true,
  keepAliveMsecs: 15000,
  proxy: proxyConfig
});

https
  .get(
    'https://i.ytimg.com/vi/<Video-ID>/sddefault.jpg',
    { agent },
    (response) => {
      response.pipe(stream);
      stream.on('finish', () => {
        stream.close();
      });
    }
  )
  .on('error', (err) => {
    console.error('Error downloading file:', err);
  });

方案2:使用axios库(自动适配系统代理)

axios会自动读取系统环境变量中的HTTP_PROXY/HTTPS_PROXY配置,无需手动写代理参数:

  1. 先安装依赖:
npm install axios
  1. 替换原有代码:
const axios = require('axios');
const fs = require('fs');

const stream = fs.createWriteStream('thumbnail.jpg');

axios({
  url: 'https://i.ytimg.com/vi/<Video-ID>/sddefault.jpg',
  method: 'GET',
  responseType: 'stream',
  // 需手动指定代理时取消注释:
  // proxy: {
  //   host: '你的代理IP',
  //   port: 你的代理端口
  // }
})
.then(response => {
  response.data.pipe(stream);
  return new Promise((resolve, reject) => {
    stream.on('finish', resolve);
    stream.on('error', reject);
  });
})
.catch(err => {
  console.error('Error downloading file:', err);
});

补充说明

  • 代理地址可以从浏览器的代理设置里获取(比如Chrome:设置→系统→打开计算机的代理设置)。
  • 如果代理有身份验证,一定要在配置中加入auth字段。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.26 17:55:02