What is the advantage of this way of reading data off a readable stream such as a request:
request.on('readable', function(){
var chunk = null;
while (null !== (chunk = request.read())) {
response.write(chunk);
};
});
vs this way without a while loop inside? Since 'readable' will just keep firing why bother with the while loop?
request.on('readable', function(){
var chunk = request.read();
if(chunk !== null){
response.write(chunk);
}
});
In the flowing mode, data can actually be lost if no consumers are available to handle it. This is why, when we have a readable stream in flowing mode, we need a data event handler.
read() Method. The readable. read() method is an inbuilt application programming interface of Stream module which is used to read the data out of the internal buffer. It returns data as a buffer object if no encoding is being specified or if the stream is working in object mode.
The good thing about Streams is how they help read large chunks of data in pieces and process them instead of reading them all at once which is the traditional way. This helps when you have limited memory space. Working with streams can be as simple as reading content from a text file using the fs (file system) module.
Q 4 - Which of the following is true about readable stream? A - Readable stream is used for read operation.
As per the API documentation:
var req = http.request(options, function(res) {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('BODY: ' + chunk);
});
});
Using the res.on('data') event you get the data when it's ready. This will allow your program to move along and do other things until the next chunk of data is ready to be processed (remember HTTP is over TCP which comes in chunks).
Using the below code might work, but why do that when it's needlessly eating CPU cycles and blocking other code from executing (remember that your Node.js JavaScript code is single-threaded). Using events is far better since it allows your JavaScript to run and process input/output without blocking the process needlessly.
request.on('readable', function(){
var chunk = null;
while (null !== (chunk = request.read())) {
response.write(chunk);
};
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With