Error: connect ETIMEDOUT at TCPConnectWrap.afterConnect [as oncomplete]

最近用node的request模块利用pipe管道下载图片,总共是561个链接。结果每次都在第330+的时候报如下的错误:
Error: connect ETIMEDOUT at TCPConnectWrap.afterConnect [as oncomplete]_第1张图片
相关代码如下:

import request from 'request'
import fs from 'fs'
import {jdbc as JDBC} from './mysql'
import {config} from "./config";
const path = './images';
const jdbc = new JDBC(config.db);
let count = 0;
let empty =  0;
if(fs.existsSync(path)){
    let files = fs.readdirSync(path);
    if(files.length){
        console.log('检测到文件残留');
        files.forEach(file => {
            fs.unlinkSync(path + '/' +file)
        });
        console.log('清除成功')
    }
}
jdbc.select('select origin_img_url,drugId from druglist').then( result => {
    result.forEach(item => {
        let url = item['origin_img_url'];
        let name = item['drugId'];
        if(url === ''){
            empty ++;
            return
        }
        const downloadStream = request(url);
        downloadStream.pipe(fs.createWriteStream(`${path}/${name}.png`).on('close', ()=> {
             console.log(`${formatTime(new Date())} 已下载${++count}张图片  ${empty ? "【跳过" + empty + "条无效数据】" : ''}${result.length} 条数据`);
          }));
    })
});
function formatTime(date){
    return `${date.getFullYear()}-${(date.getMonth()+1).toString().padStart(2, '0')}-${date.getDate().toString().padStart(2, '0')} ${date.getHours().toString().padStart(2, '0')}:${date.getMinutes().toString().padStart(2, '0')}:${date.getSeconds().toString().padStart(2, '0')}`
}

经过长时间的搜集资料和调试,发现这就是request模块里发请求时超时了…
Error: connect ETIMEDOUT at TCPConnectWrap.afterConnect [as oncomplete]_第2张图片

添加错误监听,然后重新请求吧

download.on('error', err => {
            console.error(`ERROR: 下载图片 ${name} 出错  准备重试`, JSON.stringify(err));
            if(err.code === 'ETIMEDOUT'){
                request(url).pipe(fs.createWriteStream(`${path}/${name}.png`).on('close', ()=> {
                    if(count + error +1 === result.length - empty){
                        console.log('下载完成!')
                    }else{
                        console.log(`${formatTime(new Date())} 重新下载 ${name} 成功 已成功下载${count + ++error}`);
                    }
                }));
            }
        });

参考文章:
https://github.com/request/request#requestoptions-callback
https://stackoverflow.com/questions/23632914/how-to-handle-etimedout-error
https://blog.csdn.net/duzixi/article/details/53841162
https://stackoverflow.com/questions/53339823/error-connect-etimedout-at-tcpconnectwrap-afterconnect-as-oncomplete

你可能感兴趣的:(node)