节点服务器正在关闭错误:写完后

时间:2017-05-24 13:47:55

标签: node.js stream pipe

我们正在从s3流式传输文件,它运行良好但有时(几天内随机)它给出错误并且节点实例正在关闭。我们的代码是

s3Utils.headObject(assetId, function (err, metadata) {
        if (err && err.code === 'NotFound') {
          console.log("file not found on s3: ", assetId) ;

          res.send({"success":false, "message":"file not found on s3: "+ assetId});
        } else {
          var download_file_name = assetId;
          if(metadata.Metadata && metadata.Metadata.filename) {
            download_file_name = metadata.Metadata.filename;
          }
          if(metadata.ContentLength) {
            res.setHeader('Content-Length',metadata.ContentLength);
          }

          res.setHeader('Content-disposition', 'attachment; filename=' +download_file_name);
          res.attachment(download_file_name);

          var fileStream = s3Utils.getObjectAsReadStream(assetId);
          fileStream.pipe(res);
        }
      });

我们在日志中遇到错误

    events.js:154
      throw er; // Unhandled 'error' event
      ^

Error: write after end
    at writeAfterEnd (_stream_writable.js:166:12)
    at PassThrough.Writable.write (_stream_writable.js:211:5)
    at IncomingMessage.ondata (_stream_readable.js:536:20)
    at emitOne (events.js:90:13)
    at IncomingMessage.emit (events.js:182:7)
    at IncomingMessage.Readable.read (_stream_readable.js:368:10)
    at flow (_stream_readable.js:751:26)
    at resume_ (_stream_readable.js:731:3)
    at _combinedTickCallback (internal/process/next_tick.js:74:11)
    at process._tickDomainCallback (internal/process/next_tick.js:122:9)

此错误后服务器正在关闭。 请建议。

0 个答案:

没有答案