Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

event associated with fs.createWriteStream in node.js

Tags:

stream

node.js

What event is triggered when EOF is reached while writing to a stream ? My code is as follows. and it is as per http://docs.nodejitsu.com/articles/advanced/streams/how-to-use-fs-create-write-stream

But surprisingly my 'end' event is never fired. When I checked http://nodejs.org/api/stream.html#stream_event_end, I see that writable stream does not have any event on 'end'


var x = a1.jpg; var options1 = {'url': url_of_an_image, 'encoding': null}; var r = request(options1).pipe(fs.createWriteStream('/tmp/imageresize/'+x));  r.on('end', function(){     console.log('file downloaded to ', '/tmp/imageresize/'+x); } 

How do I capture the EOF event ?

like image 592
user644745 Avatar asked Oct 31 '12 10:10

user644745


People also ask

What is FS createWriteStream in node JS?

The function fs. createWriteStream() creates a writable stream in a very simple manner. After a call to fs. createWriteStream() with the filepath, you have a writeable stream to work with. It turns out that the response (as well as the request) objects are streams.

Which method of FS module is used to read a file in node JS?

The fs. read() method reads the file using the file descriptor and stores in the buffer.

Which of the following is use to create write strean?

createWriteStream() Method. The createWriteStream() method is an inbuilt application programming interface of fs module which allows to quickly make a writable stream for the purpose of writing data to a file.

What method is available in the node js FS module for writing data to a file?

TABLE OF CONTENTS. The easiest way to write to files in Node. js is to use the fs. writeFile() API.


2 Answers

Updated 30 Oct 2013

Readable Steams emit close event when the underlying resource done writing.

r.on('close', function(){   console.log('request finished downloading file'); }); 

But if you want to catch the moment when fs finished writing data to the disc, you need Writeable Stream finish event:

var w = fs.createWriteStream('/tmp/imageresize/'+x);  request(options1).pipe(w);  w.on('finish', function(){   console.log('file downloaded to ', '/tmp/imageresize/'+x); }); 
like image 129
Leonid Beschastny Avatar answered Oct 04 '22 00:10

Leonid Beschastny


This is really old but this still saved me a lot of time.

  1. Solution from leonid-beschastny is correct. However, there is a very subtle difference in the complete sequence of the calls being made. The solutions are all from the experts and a newbie like me gets lost - due to simple differences like - how a stream is being created or the objects missed in the description.

  2. Also, this is a generic solution to a lot of other scenarios. When you are piping to a writableStream - and by the time async function that contains this operation - returns. The streaming is not complete - so the probable image/document/pdf that is being piped to - is not yet created. It will be of size 0 KB.

  3. Sharing here in simple steps.

    a. createPdfKitDocument with your definition as required.  b. Create the writeStream the way you want as per your design.  c. Pipe your prepared document to this stream.  d. Add this finish event to your writeStream.  e. Most important - without this it doesn't work. 

Here

var pdfStream = printer.createPdfKitDocument(docDefinition); //a let writerStream = fs.createWriteStream(fullfileName); //b pdfStream.pipe(writerStream); //c writerStream.on('finish', function () {     //This could be your callback function.     callback()); }) //d pdfStream.end(); //e 
  1. I also referred to these links - they might help someone a. PDF download fails using PDFMake in NodeJS This gave me the clue to post.

  2. Main issue is - createWriteStream is not finished [due to whatever reasn] - by the time the async function that contains it - returns.

  3. Also leonid-beschastny is right in saying that race condition won't occur - I test my code by swapping the position of the pdfStream.pipe(writerStream);

and

writerStream.on('finish', function () {     //This could be your callback function.     callback()); }) 

Hope it helps few more people.

like image 20
KManish Avatar answered Oct 03 '22 23:10

KManish