简体   繁体   中英

“Unhandled stream error in pipe” - Too many file downloads using request.js with express

I am trying to download more than one image.

Sometimes downloading 10 photos, sometimes downloading 500 photos, the following this error.

I have to find a solution.

internal/streams/legacy.js:59
  throw er; // Unhandled stream error in pipe.
  ^

Error: read ECONNRESET
 at _errnoException (util.js:1024:11)
 at TCP.onread (net.js:615:25)

Datas has image download url's.

    let arrDatas = [];

    datas.map(data => {
      arrDatas.push(
        new Promise((resolve, reject) => {
          request.head(data.url, (err, res, body) => {
            if (!err && res.statusCode === 200) {
              let imgType;
              if (
                res.headers["content-type"] ===
                "application/octet-stream"
              ) {
                imgType = "jpeg";
              } else {
                imgType = res.headers["content-type"].split("/")[1];
              }

              request({
                url: data.url,
                headers: {
                  "Keep-Alive": "max=2000"
                }
              })
                .pipe(
                  fs.createWriteStream(
                    "server/downloads/" + createUniqueSHA1String() + "." + imgType
                  )
                )
                .on("close", close => {
                  resolve(true);
                });
            } else {
              resolve(false);
            }
          });
        })
      );
    });


     Promise.all(arrDatas).then(result => {
      res.json(result);
     })
     .catch(err => {
       res.status(500).json(err.message);
     });

Can anyone help me ?

似乎另一个端点过早地结束了连接,请看一下:Node js ECONNRESET

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM