node.js 处理管道完成的回调
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11447872/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
callback to handle completion of pipe
提问by user644745
I am using the following node.js code to download documents from some url and save it in the disk. I want to be informed about when the document is downloaded. i have not seen any callback with pipe.Or, Is there any 'end' event that can be captured on completion of download ?
我正在使用以下 node.js 代码从某个 url 下载文档并将其保存在磁盘中。我想知道文档何时下载。我还没有看到任何带有管道的回调。或者,是否有任何可以在下载完成时捕获的“结束”事件?
request(some_url_doc).pipe(fs.createWriteStream('xyz.doc'));
回答by Pickels
Streams are EventEmitters so you can listen to certain events. As you said there is a finishevent for request (previously end).
流是EventEmitters,因此您可以收听某些事件。正如你所说,有一个finish请求事件(以前end)。
var stream = request(...).pipe(...);
stream.on('finish', function () { ... });
For more information about which events are available you can check the stream documentation page.
有关哪些事件可用的更多信息,您可以查看流文档页面。
回答by smartwjw
Based nodejs document, http://nodejs.org/api/stream.html#stream_event_finish,
it should handle writableStream's finishevent.
基于 nodejs 文档http://nodejs.org/api/stream.html#stream_event_finish,它应该处理 writableStream 的finish事件。
var writable = getWriteable();
var readable = getReadable();
readable.pipe(writable);
writable.on('finish', function(){ ... });
回答by ruX
Code snippet for piping content from web via http(s) to filesystem. As @starbeamrainbowlabs noticed event finishdoes job
用于通过 http(s) 从 web 管道内容到文件系统的代码片段。正如@starbeamrainbowlabs 注意到事件finish确实起作用
var tmpFile = "/tmp/somefilename.doc";
var ws = fs.createWriteStream(tmpFile);
ws.on('finish', function() {
// pipe done here, do something with file
});
var client = url.slice(0, 5) === 'https' ? https : http;
client.get(url, function(response) {
return response.pipe(ws);
});
回答by Abu Shumon
I found an a bit different solution of my problem regarding this context. Thought worth sharing.
我在这个上下文中发现了我的问题的一个有点不同的解决方案。思想值得分享。
Most of the example create readStreamsfrom file. But in my case readStreamhas to be created from JSONstring coming from a message pool.
大多数示例readStreams从文件创建。但在我的情况下readStream,必须从JSON来自消息池的字符串创建。
var jsonStream = through2.obj(function(chunk, encoding, callback) {
this.push(JSON.stringify(chunk, null, 4) + '\n');
callback();
});
// message.value --> value/text to write in write.txt
jsonStream.write(JSON.parse(message.value));
var writeStream = sftp.createWriteStream("/path/to/write/write.txt");
//"close" event didn't work for me!
writeStream.on( 'close', function () {
console.log( "- done!" );
sftp.end();
}
);
//"finish" event didn't work for me either!
writeStream.on( 'close', function () {
console.log( "- done!"
sftp.end();
}
);
// finally this worked for me!
jsonStream.on('data', function(data) {
var toString = Object.prototype.toString.call(data);
console.log('type of data:', toString);
console.log( "- file transferred" );
});
jsonStream.pipe( writeStream );
回答by blak3r
Here's a solution that handles errors in requests and calls a callback after the file is written:
这是处理请求中的错误并在写入文件后调用回调的解决方案:
request(opts)
.on('error', function(err){ return callback(err)})
.pipe(fs.createWriteStream(filename))
.on('finish', function (err) {
return callback(err);
});

