Node.js – Events


Node.js – Events



”;


When JavaScript is used inside HTML script, it generally handles the user-generated events such as button press or mouse clicks. The core API of Node.js is an asynchronous event-driven architecture. However, unlike the client-side JavaScript, it handles the events on the server, such as File io operations, server”s request and responses etc.

The net.Server object emits an event each time a peer connects to it.

The ReadStream referring to a file emits an event when the file is opened and whenever data is available to be read.

Node.js identifies several types of events. Each event can be attached to a callback function. Whenever an event occurs, the callback attached to it is triggered. The Node.js runtime is always listening to events that may occur. When any event that it can identify occurs, its attached callback function is executed.

The Node.js API includes events module, consisting mainly the EventEmitter class. An EventEmmiter object triggers (or emits) a certain type of event. You can assign one or more callbacks (listeners) to a certain type of event. whenever that event triggers, all the registered callbacks are fired one by one in order to which they were registered.


Event Emitter

These are the steps involved in event handling in Node.js API.

First, import the events module, and declare an object of EventEmitter class


// Import events module
var events = require(''events'');

// Create an eventEmitter object
var eventEmitter = new events.EventEmitter();

Bind an event handler with an event with the following syntax −


// Bind event and event  handler as follows
eventEmitter.on(''eventName'', eventHandler);

To fire the event programmatically −


// Fire an event 
eventEmitter.emit(''eventName'');

Example

Given below is a simple example that binds two listeners to the on event of EventEmitter class


// Import events module
var events = require(''events'');

// Create an eventEmitter object
var eventEmitter = new events.EventEmitter();

// Create an event handler as follows
var connectHandler = function connected() {
   console.log(''connection successful.'');
}

// Bind the connection event with the handler
eventEmitter.on(''connection'', connectHandler);
 
// Bind the data_received event with the anonymous function
eventEmitter.on(''data_received'', function(){
   console.log(''data received successfully.'');
});

// Fire the connection event 
eventEmitter.emit(''connection'');

// Fire the data_received event 
eventEmitter.emit(''data_received'');
console.log("Program Ended.");

Output


connection successful.
data received successfully.
Program Ended.

Any asynchronous function in Node Application accepts a callback as the last parameter. The callback function accepts an error as the first parameter.

Create a text file named input.txt with the following content.


Tutorials Point is giving self learning content
to teach the world in simple and easy way!!!!!

Create a js file named main.js having the following code −


var fs = require("fs");
fs.readFile(''input.txt'', function (err, data) {
   if (err) {
      console.log(err.stack);
      return;
   }
   console.log(data.toString());
});
console.log("Program Ended");

Here fs.readFile() is a async function that read a file. If an error occurs during the read operation, then the err object will contain the corresponding error, else data will contain the contents of the file. readFile passes err and data to the callback function after the read operation is complete, which finally prints the content.


Program Ended
Tutorials Point is giving self learning content
to teach the world in simple and easy way!!!!!

Advertisements

”;

Leave a Reply

Your email address will not be published. Required fields are marked *