Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 在节点中检测writeStream的结束_Node.js_File Io_Stream - Fatal编程技术网

Node.js 在节点中检测writeStream的结束

Node.js 在节点中检测writeStream的结束,node.js,file-io,stream,Node.js,File Io,Stream,这就是我得到的,我不断得到一个错误,因为当我按顺序执行时,文件还不存在 writeStream关闭后,如何触发操作 var fs = require('fs'), http = require('http'); http.createServer(function(req){ req.pipe(fs.createWriteStream('file')); /* i need to read the file back, like this or something:

这就是我得到的,我不断得到一个错误,因为当我按顺序执行时,文件还不存在

writeStream关闭后,如何触发操作

var fs = require('fs'), http = require('http');
http.createServer(function(req){
    req.pipe(fs.createWriteStream('file'));


    /* i need to read the file back, like this or something: 
        var fcontents = fs.readFileSync(file);
        doSomethinWith(fcontents);
    ... the problem is that the file hasn't been created yet.
    */

}).listen(1337, '127.0.0.1');
可写流具有在刷新数据时发出的事件

尝试以下方法

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');

虽然我不会重读文件。您可以使用来创建流处理器。

然后,您是否会在完成事件发生后分配完成处理程序?即,在读取代码时,您是否希望执行.on('finish'…行,在.pipe(f)one?:)之前),我希望先绑定到事件,但该问题中的注释是正确的。上面的代码对你有用吗?不知道,还没试过。