Tôi tìm thấy một giải pháp khác nhau một chút vấn đề của tôi liên quan đến bối cảnh này. Tư tưởng đáng chia sẻ.
Hầu hết ví dụ tạo readStreams
từ tệp. Nhưng trong trường hợp của tôi readStream
phải được tạo từ JSON
chuỗi đến từ một nhóm tin nhắn.
var jsonStream = through2.obj(function(chunk, encoding, callback) {
this.push(JSON.stringify(chunk, null, 4) + '\n');
callback();
});
// message.vale --> valeu/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);
'var r = yêu cầu (...) trên ("kết thúc", function () {/ * CALLBACK * /}). Pipe (...); ' –
cho tôi sự kiện 'close', thay vì 'end' works' r.on ('close'), function() {... }) ' – Judson
Sự kiện 'kết thúc' hiện là 'kết thúc' Sự kiện ống: http://nodejs.org/api/stream.html#stream_event_finish –