Node.js“请求”;未处理的错误,节点:事件:368

发布于 2025-01-11 01:55:59 字数 1835 浏览 0 评论 0原文

我遇到了一个重复出现的错误,导致我的程序崩溃。 在程序中,我一直在发出异步请求(我应该说是一一请求) 要从互联网上下载特定图像,这是我的请求代码:

    const urlDownload = (uri, filename, callback) => {
    try {
        console.log('start url');
        console.log(uri);
        request.head(uri, function(err, res, body){
            if(err) {
                console.log('request error!');
                console.log(err);
            } else {
                request(uri).pipe(fs.createWriteStream(filename)).on('close', callback);
                console.log('end url');
            }
        //   console.log('content-type:', res.headers['content-type']);
        //   console.log('content-length:', res.headers['content-length']);

        });
    } catch(e) {
        console.log('urlDownload issue');
        console.log(e);
    }
}

现在,有时它不会崩溃,但我觉得 50% 或更多的时间它会出现这个确切的错误:

node:events:368
      throw er; // Unhandled 'error' event
      ^

Error: aborted
    at connResetException (node:internal/errors:691:14)
    at TLSSocket.socketCloseListener (node:_http_client:407:19)
    at TLSSocket.emit (node:events:402:35)
    at node:net:687:12
    at TCP.done (node:_tls_wrap:580:7)
Emitted 'error' event on Request instance at:
    at Request.onerror (node:internal/streams/legacy:62:12)
    at Request.emit (node:events:390:28)
    at IncomingMessage.<anonymous> (/home/mrz/Desktop/DEVELOPMENT/node_modules/request/request.js:1079:12)
    at IncomingMessage.emit (node:events:390:28)
    at emitErrorNT (node:internal/streams/destroy:157:8)
    at emitErrorCloseNT (node:internal/streams/destroy:122:3)
    at processTicksAndRejections (node:internal/process/task_queues:83:21) {
  code: 'ECONNRESET'
}

我不知道该怎么办做了,试图搜索它并没有发现什么, 它尝试下载的 URL 也没有问题!

根据我所做的控制台日志,我认为它在成功结束请求过程后似乎崩溃了,因为我确实得到了输出,然后它崩溃了,这只是猜测,我不知道发生了什么。

I'm getting a reoccuring error that crashes my program.
In the program, I'm consistantly making async requests (one by one I should say)
to download specific images off the internet, this is my code for the request:

    const urlDownload = (uri, filename, callback) => {
    try {
        console.log('start url');
        console.log(uri);
        request.head(uri, function(err, res, body){
            if(err) {
                console.log('request error!');
                console.log(err);
            } else {
                request(uri).pipe(fs.createWriteStream(filename)).on('close', callback);
                console.log('end url');
            }
        //   console.log('content-type:', res.headers['content-type']);
        //   console.log('content-length:', res.headers['content-length']);

        });
    } catch(e) {
        console.log('urlDownload issue');
        console.log(e);
    }
}

Now, sometimes it doesn't crash, but I feel like 50% or more of the times it does with this exact error:

node:events:368
      throw er; // Unhandled 'error' event
      ^

Error: aborted
    at connResetException (node:internal/errors:691:14)
    at TLSSocket.socketCloseListener (node:_http_client:407:19)
    at TLSSocket.emit (node:events:402:35)
    at node:net:687:12
    at TCP.done (node:_tls_wrap:580:7)
Emitted 'error' event on Request instance at:
    at Request.onerror (node:internal/streams/legacy:62:12)
    at Request.emit (node:events:390:28)
    at IncomingMessage.<anonymous> (/home/mrz/Desktop/DEVELOPMENT/node_modules/request/request.js:1079:12)
    at IncomingMessage.emit (node:events:390:28)
    at emitErrorNT (node:internal/streams/destroy:157:8)
    at emitErrorCloseNT (node:internal/streams/destroy:122:3)
    at processTicksAndRejections (node:internal/process/task_queues:83:21) {
  code: 'ECONNRESET'
}

I don't know what to do, tried to search about it and found nothing really,
nothing wrong with the URL it tries to download too!

based on the console logs I have made, it seems like it crashes AFTER it ends the request process successfully I think, because I do get the output, and then it crashes, that's just speculations, I have no idea what's going on.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文