Detecting the end of a writeStream in Node

后端 未结 1 857
盖世英雄少女心
盖世英雄少女心 2021-02-07 05:47

This is what I\'ve got, and I keep getting an error because the file doesn\'t exist yet when I just do it sequentially.

How can I trigger an action upon the writeStream

1条回答
  •  孤独总比滥情好
    2021-02-07 06:04

    Writable streams have a finish event that is emitted when the data is flushed.

    Try the following;

    var fs = require('fs'), http = require('http');
    
    http.createServer(function(req, res){
        var f = fs.createWriteStream('file');
    
        f.on('finish', function() {
            // do stuff
            res.writeHead(200);
            res.end('done');
        });
    
        req.pipe(f);
    }).listen(1337, '127.0.0.1');
    

    Though I wouldnt re-read the file. You can use through to create a stream processor.

    0 讨论(0)
提交回复
热议问题