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?

2 Answers2

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);
});
Lucas Neves
  • 31
  • 1
  • 4
0

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

var file = fs.createReadStream("veg.log", "utf8");
Colt
  • 1,939
  • 6
  • 20
  • 25
Pidlik
  • 1