javascript - 管道传输后如何写入流?

标签 javascript node.js stream

在 NodeJS 中,我使用 ExpressJS (其中 res 是可写流)

const readableStream = someAsyncTask();
readableStream.pipe(res);
readableStrean.on('end', () => {
    res.write('a bit more');
    res.end();
});

这会导致:

uncaught exception Error: write after end

所以我假设管道导致可写流关闭。如何将可读流传输到输出,然后当该流结束时,将附加数据传输到输出?

最佳答案

来自documentation

readable.pipe(destination[, options])

  • destination <stream.Writable> The destination for writing data
  • options <Object> Pipe options
    end <Boolean> End the writer when the reader ends. Defaults to true.
    ...

如果您不希望它在管道传输时结束,请传递 {end : false}作为选项

readableStream.pipe(res, { end: false });

关于javascript - 管道传输后如何写入流?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39804007/

相关文章:

javascript - 根据鼠标位置自动滚动 div

node.js - 结合 Nodejs Net 套接字和 Socket IO

c# - 流读取问题

java - 使用 Java 打开 Arduino 上的 LED

python - 波开 : redirect stderr and stdout to single stream

javascript - PerformanceObserver 无法测量重定向请求时间

javascript - 使用按钮而不是单页网络应用程序的链接

node.js - 安装npm时出现错误

javascript - .get() 需要回调函数,但得到一个 [object Undefined]

javascript - 如何从模板或带有占位符的片段创建 bootstrap html 库以轻松更改类?