Nodejs Course

Every action on a computer is an event. Like when a connection is made or a file is opened, or a button is pressed.
Objects in Node.js can emit events, like the readStream object fires events when opening and closing a file.
To listen an event, use the on() method.
var fs = require('fs');
var rs = fs.createReadStream('./test/file.txt');
rs.on('open', ()=>{
  console.log('The file is open');
});

Events Module in Node.js

Node.js has a built-in module, called "events", that can be used to create, emit, and listen for your own events.
To include the Events module use the require() method. All event properties and methods are an instance of an EventEmitter object.
To access these properties and methods, create an EventEmitter object. Then, you can register events and assign listeners to your own events with the EventEmitter object.
To emit an event, use the emit() method.
- Example: Calling a funtion when the "voice" event is emited:
var events = require('events');
var evEm = new events.EventEmitter();

//Create a listener function
var onVoice = ()=>{
  console.log('I hear a voice');
}

//Assign the listener function to an event
evEm.on('voice', onVoice);

//Emit the 'voice' event
evEm.emit('voice');

Passing arguments to the listener function

The emitter.emit() method allows to pass arguments to the listener functions, after the event name.
var events = require('events');
var evEm = new events.EventEmitter();

//Create a listener function with 2 parameters
var onVoice = (a, b)=>{
  console.log('I hear a voice with: '+ a +' and '+ b);
}

//Assign the listener function to an event
evEm.on('voice', onVoice);

//Emit the 'voice' event, passing two arguments: 'love', 'voice'
evEm.emit('voice', 'love', 'peace');

Removing event listener

To remove a registered event use the emitter.removeListener(eventName, listener) method, .
var events = require('events');
var evEm = new events.EventEmitter();
let nre =0;

//Create a listener function with 2 parameters
var onVoice = (a)=>{
  console.log('I hear a voice with: '+ a);
  nre++;

  //removes onVoice listener for 'voice' event
  if(nre >=2) evEm.removeListener('voice', onVoice);
}

//Assign the listener function to an event
evEm.on('voice', onVoice);

//Emit the 'voice' event three times
evEm.emit('voice', 'love');
evEm.emit('voice', 'peace');
evEm.emit('voice', 'joy');

//Prints:
// I hear a voice with: love
// I hear a voice with: peace
- For more details about events module, see the Events Node.js Documentation page.

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which meta tag provides a short description of the page?
<meta content="..."> <meta description="..."> <meta http-equiv="...">
<meta name="description" content="70-160 characters that describes the content of the page" />
Which CSS property is used to stop the wrapping effect of the "float"?
clear text-align position
#some_id {
  clear: both;
}
Click on the method which gets an array with all the elements in the document that have a specified tag name.
getElementsByName() getElementById() getElementsByTagName()
var divs = document.getElementsByTagName("div");
var nr_divs = divs.length;
alert(nr_divs);
Indicate the PHP function which returns the number of elements in array.
is_[) count() strlen()
$arr =[7, 8, "abc", 10);
$nri = count($arr);
echo $nri;        // 4
Events, EventEmitter object

Last accessed pages

  1. PHP Unzipper - Extract Zip, Rar Archives (31816)
  2. Arrays in ActionScript 3 (3091)
  3. String Object (667)
  4. Register and show online users and visitors (39428)
  5. PhpSpreadsheet - Read, Write Excel and LibreOffice Calc files (26057)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (202)
  2. Read Excel file data in PHP - PhpExcelReader (66)
  3. The Mastery of Love (56)
  4. PHP Unzipper - Extract Zip, Rar Archives (52)
  5. Working with MySQL Database (34)
Chat
Chat or leave a message for the other users
Full screenInchide