在 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
所以我假设管道导致可写流关闭。如何将可读流传输到输出,然后当该流结束时,将附加数据传输到输出?
最佳答案
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/