写入完成之前通过管道传输节点流

写入完成之前通过管道传输节点流

本文介绍了写入完成之前通过管道传输节点流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的缓冲流在完成向其写入"response.Body"之前就已通过管道传输,这会引发以下错误:错误:写完后在writeAfterEnd

my buffer stream is getting piped before its completed writing 'response.Body' into it which is throwing this error:Error: write after end at writeAfterEnd

var bufferStream = new stream.PassThrough();
bufferStream.end(response.Body);
        var stream = bufferStream.pipe(fs.createWriteStream(dlFile));
            stream.on('finish', function () {
                console.log('finished writing stream');
                cb(null, response);
            });

我不能使用事件'end',因为它是PassThrough流.如何确保第一个过程完成?

I cant use the event 'end' because it is a PassThrough stream. How do I make sure the first process is completed?

推荐答案

在将数据通过管道传输到可写流之后,应将其写入.那就是 bufferStream.pipe 行应该在 bufferStream.end 行之前:

You should write your data after piping it to a writable stream. That is bufferStream.pipe line should be before the bufferStream.end line:

var bufferStream = new stream.PassThrough();
// pipe first
var stream = bufferStream.pipe(fs.createWriteStream(dlFile))
  .on('finish', function () {
    console.log('finished writing stream');
    cb(null, response);
  });
//now write data
bufferStream.end(response.Body);

这篇关于写入完成之前通过管道传输节点流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 02:21