How to close a readable stream (before end)?

前端 未结 9 1356
名媛妹妹
名媛妹妹 2020-11-29 03:16

How to close a readable stream in Node.js?

var input = fs.createReadStream(\'lines.txt\');

input.on(\'data\', function(data) {
   // after closing the strea         


        
9条回答
  •  無奈伤痛
    2020-11-29 03:41

    This code here will do the trick nicely:

    function closeReadStream(stream) {
        if (!stream) return;
        if (stream.close) stream.close();
        else if (stream.destroy) stream.destroy();
    }
    

    writeStream.end() is the go-to way to close a writeStream...

提交回复
热议问题