Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

createWriteStream vs writeFile?

What is the basic difference between these two operations ?

someReadStream.pipe(fs.createWriteStream('foo.png'));

vs

someReadStream.on('data', function(chunk) { blob += chunk } );
someReadStream.on('end', function() { fs.writeFile('foo.png', blob) });

When using request library for scraping, I can save pics (png, bmp) etc.. only with the former method and with the latter one there is same gibbersh (binary) data but image doesn't render.

How are they different ?

like image 676
Randy Avatar asked Jan 05 '13 08:01

Randy


People also ask

Should I use writeFile or writeFileSync?

Creating and writing files with writeFileSync is only recommended for debugging purposes, just like every other synchronous function in Node. As such, you should use the asynchronous function writeFile for creating and writing files in real-world projects.

What is FS createWriteStream?

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.

Does createWriteStream overwrite?

createWriteStream() in append mode overwrites files on Windows #1897.

Is FS writeFile a promise?

Promise version of fs. writeFile: Asynchronously writes data to a file, replacing the file if it already exists.


1 Answers

When you are working with streams in node.js you should prefer to pipe them.

According to Node.js’s stream-event docs, data events emit either buffers (by default) or strings (if encoding was set).

When you are working with text streams you can use data events to concatenate chunks of string data together. Then you'll be able to work with your data as one string.

But when working with binary data it's not so simple, because you'll receive buffers. To concatenate buffers you use special methods like Buffer.concat. It's possible to use a similar approach for binary streams:

var buffers = [];
readstrm.on('data', function(chunk) {
    buffers.push(chunk);
});
readstrm.on('end', function() {
    fs.writeFile('foo.png', Buffer.concat(buffers));
});

You can notice when something goes wrong by checking the output file's size.

like image 81
Leonid Beschastny Avatar answered Nov 05 '22 22:11

Leonid Beschastny