これは私が得たものであり、順次実行するだけではファイルがまだ存在しないため、エラーが発生し続けます。
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');
書き込み可能なストリームには、データがフラッシュされるときに発行される finish イベントがあります。
以下を試してください。
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');
ファイルを再度読み取ることはしませんが。 through を使用して、ストリームプロセッサを作成できます。