3

I am attempting to view a log with a program called 'logger'. When I run node logger I get the following error:

<Buffer 66 72 75 69 74 69 73 68 79 0a>

The 'logger' program file looks as follows:

var fs = require('fs');

var file = fs.createReadStream("veg.log");

file.on('readable', function() {
    var stream = null;
    while(null !== (stream = file.read()))  {
      stream.toString();
      console.log(stream);
      }
});

Does anyone have an idea what's causing the error? The log file is just plain text. Why is this causing an error with my buffer and how do I find out which buffer it is and what's causing it to be incorrectly accessed?

1

The canonical procedure is to use a buffer to collect chunks of data by listening to on('data') events. When the stream is finished, the on('end') event will be emitted, and then you can do something with the buffer.

This code should do what you need:

var fs = require('fs');

var buffer = '';
var file = fs.createReadStream("veg.log");
file.on('data', function(chunk) {
  buffer += chunk;
});
file.on('end', function () {
  console.log(buffer);
});
0

You need to specify a buffer option for the createReadStream function. Like so:

var file = fs.createReadStream("veg.log", "utf8");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.