Content of the logger module
const EventEmitter = require('events');

class Logger extends EventEmitter {
  log(message) {
    
    // Log the message
    console.log(message);
    
    // Raise an event
    this.emit('messageLogged', { id: 1, url: 'http://' });
  }
}

module.exports = Logger;
Content of the index file
const Logger = require('./logger');
const logger = new Logger();

// Register a listener
logger.on('messageLogged', function(arg) {
  console.log('Listener called', arg);
});

logger.log('message');
by Valeri Tandilashvili
2 years ago
Node
Events
Node.js Tutorial for Beginners - 1 Hour
0
Pro tip: use ```triple backticks around text``` to write in code fences