user7453724
user7453724

Reputation:

“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 ?

Upvotes: 2

Views: 7312

Answers (1)

André Baldo
André Baldo

Reputation: 26

It seems that the other endpoint is ending prematurely the connection, please take a look on this: Node js ECONNRESET

Upvotes: 1

Related Questions