Results: 9
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');
Event emitter raises the event called
messageLogged
and passes the object as an argument
const EventEmitter = require('events');
const emitter = new EventEmitter();

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

// Raise an event & passes the object
emitter.emit('messageLogged', {key1: 'Value 1', key2: 'Value 2'});
Event emitter raises the event called
messageLogged
and passes parameters
const EventEmitter = require('events');
const emitter = new EventEmitter();

// Register a listener
emitter.on('messageLogged', function(arg1, arg2) {
  console.log('Listener called', arg2, arg1);
});

// Raise an event
emitter.emit('messageLogged', 'arg-first', 'arg-second');
Event emitter raises the event called
messageLogged
const EventEmitter = require('events');
const emitter = new EventEmitter();

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

// Raise an event
emitter.emit('messageLogged');
Reads the current directory synchronously
const fs = require('fs')

// With Sync function
let files = fs.readdirSync('./')
console.log(files)
Reads the current directory asynchronously
const fs = require('fs')

// With Async function
fs.readdir('./', function(err, files){
    if (err) {
        console.log('Error', err)
    } else {
        console.log(files)
    }
})
Prints total and free memory using OS built-in module:
const os = require('os')

let totalMemory = os.totalmem()
let freeMemory = os.freemem()

console.log(`Total memory: ${totalMemory}`)
console.log(`Free memory: ${freeMemory}`)
logger.js
file exports object which contains only the
log()
function. Content of the
logger.js
file:
function log(message) {
  console.log(message)
}

module.exports.log = log
The
log()
function is called in the
index.js
file:
const logger = require('./logger')

logger.log('some text')
logger.js
file exports
log()
function. Content of the
logger.js
file:
function log(message) {
  console.log(message)
}

module.exports = log
The
log()
function is called in the
index.js
file:
const log = require('./logger')

log('some text')
Each
.js
file is a
module
itself, which contains important information about the file. If we
console.log(module)
, it will print the following object:
Module {
  id: '.',
  path: '/home/runner/xrfnkv6ixs',
  exports: {},
  parent: null,
  filename: '/home/runner/xrfnkv6ixs/index.js',
  loaded: false,
  children: [],
  paths: [
    '/home/runner/xrfnkv6ixs/node_modules',
    '/home/runner/node_modules',
    '/home/node_modules',
    '/node_modules'
  ],
  _compile: [Function]
}
exports
key includes the content (variable / function / object) that's going to be publicly available for other modules
filename
key is equal to the module file location ...
Results: 9