0

How to read the contents of a file by using the fs core module of Node, and log the contents into the console using events. I tried the below code but yet not worked. Is there any other way? I'm new to node

events= require('events');
var eventEmitter = new events.EventEmitter();
fs = require('fs');


var Myfunc = function () {
 fs.readFile('./new-dir/sample.txt', function(err, data){
  if(err){
   console.log(err);
  }
  else{
   console.log(data.toString());
  }
 });
}

eventEmitter.on('MyEvent', Myfunc);
eventEmitter.emit('MyEvent');
Fahad
  • 175
  • 2
  • 6
  • 18

0 Answers0