user200400 user200400 - 1 year ago 70
Javascript Question

About reading data using streams in Node

On tutorial I found this example about reading data using streams

var fs = require('fs');
var readableStream = fs.createReadStream('file.txt');
var data = '';

readableStream.on('data', function(chunk) {

readableStream.on('end', function() {

I have couple questions:

1) Since one does
data+= chunk
this means the chunk is string right?
otherwise how would this concatenation work?

2) But if 1) is true and
is string, why was encoding not set?

3) How would you modify this example to read binary file? Surely the
concatenation would not work in this case if data is binary?

Can someone please clarify this confusions?

Answer Source

fs.createReadStream() takes an optional second argument which is an options object where the encoding for reading the stream can be configured. If no encoding is set, then it defaults to utf8 string. It took a while to find that info because it is not in the doc, but it is in the code here.

So, if you don't pass the encoding as a property of the options argument to fs.createReadStream(), then it defaults to utf8. if you wanted to handle binary data, then you can configure the encoding to null. Streams also have the ability to call the .setEncoding(...) method to specifically set their encoding, though it is normally set in the constructor for the stream since a stream usually doesn't change its encoding after being created.

So, the encoding for the stream determines whether you are passed a utf8 string or a buffer or some other encoding type of data. And, if you had no encoding (e.g. binary), then you would be passed a Buffer object instead of a string when new data is available.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download