Web Programming TutorialsLearn How to Build Domain Module Utility Modules in NodeJS

Learn How to Build Domain Module Utility Modules in NodeJS

In this article, we are going to discuss building the Node.js Domain module. A Domain module is used to intercept unhandled error. We can intercept these unhandled error through internal binding or external binding. The interception of errors are required as they will halt the Node.js application if it remains unhandled. The following is the explanation of internal and external bindings.

Internal Binding – Internal binding has the error emitter which executes its code within the run method of a domain.
External Binding – External binding has the error emitter which is added explicitly to a domain using it’s add method.

The domain module can be imported with the help of the following syntax. The domain class is a child class of EventEmitter. We can handle the errors that it catches or listen to its error event with the help of the following syntax

// Command to import Domain Module in Node.js.

var domain = require("domain");
var child = domain.create();

Methods present in the Domain Module of node.js
The following are the basic methods and their descriptions which are present in the Domain Module of Node.js.

Sr No

Method

Description

1.

domain.run (function)

This method of domain module is used to run the supplied function in the context of the domain. It, implicitly binds all the event emitters, timers, and low level requests which are created in that context.

2.

domain.add (emitter)

This method of domain module is used to add an emitter to the domain explicitly. When an error or an error event is thrown by any event, handlers called by the emitter then it will be routed to the domain’s error event as an implicit binding.

3.

domain.remove (emitter)

This method of the domain module acts opposite to the domain.add (emitter) and is used to remove the domain handling from the specified emitter.

4.

domain.bind (callback)

This method is used to bind the error events which are thrown by the returned function when it is called. Here, the returned function will be a wrapper around the supplied callback function.

5.

domain.intercept (callback)

This method of the domain module acts similar to the domain.bind (callback) method, but it is capable of intercepting error objects sent as the first argument to the function.

6.

domain.enter ()

This method of the domain module is used by the run, bind, and intercept methods to set the active domain.

  • It is used to set domain.active and process.domain to the domain.

  • It is used to push the domain onto the domain stack managed by the domain module implicitly.

  • It is used to delimit the beginning of a chain of asynchronous calls and I/O operations bound to a domain.

7.

domain.exit ()

This method of the domain module is used to exit the current domain, and popping it off the domain stack.

  • It is important to make sure that the current domain is exited when the execution switches to the context of a different chain of asynchronous calls.

  • It is used to delimit the end to the chain of asynchronous calls and I/O operations bound to a domain.

8.

domain.dispose ()

This method of the domain module is used to dispose the domain. When it is called, the domain will no longer be used by callbacks bound into the domain through run, bind, or intercept. It will emit a dispose event.

Properties of Domain Module
The following is the property of the domain module.

S r No.

Property

Description

1.

domain.members

This property is to explicitly add an array of timers and event emitters to the domain

Example on use of Domain Module in Node.js Application
In the following example, we are going to demonstrate the use of the above Domain module methods. Here, we are going to create one js file named DomainModule.js which has the following code.

DomainModuleServer.js

// Import events and domain.
var events = require("events");
var domain = require("domain");
//-----------------------------------An example of the Explicit binding.
var emitter1st = new events.EventEmitter();
// Create a domain 1st
var domain1st = domain.create();
domain1st.on('error', function(err){
   console.log("domain1 handled this error ("+err.message+")");
});
domain1st.add(emitter1st);
emitter1st.on('error', function(err){
   console.log ("The listener will handle this error ("+err.message+")");
});
emitter1st.emit ('error', new Error ('It is to be handled by the listener'));
emitter1st.removeAllListeners ('error');
emitter1st.emit ('error', new Error('It is to be handled by the 1st domain'));
domain1st.remove(emitter1st);
emitter1st.emit ('error', new Error ('It is converted to exception. Therefore the system will crash.'));

//------------------------------------An example of the Implicit binding.
var domain2nd = domain.create();
domain2nd.on('error', function(err){
   console.log("2nd domain will handle this error ("+err.message+")");
});
domain2nd.run(function(){
   var emitter2nd = new events.EventEmitter();
   emitter2nd.emit('error', new Error('It is to be handled by the 2nd domain'));   
});

Explanation of the code
In the above code, we are demonstrating two types of bindings i.e. Explicit binding and Implicit binding.

Explicit Binding
• In the explicit binding, we are creating domain1st domain and calling the function on the domain instance for an ‘error’ event.
• We are adding an emitter emitter1st to the domain domain1st. We are calling the function on the emitter instance an ‘error’ event.
• After this, we are using emitter to emit and remove ‘error’ events to demonstrate explicit binding in the domain module.

Implicit Binding
• In the implicit binding, we are creating domain2nd domain and calling the function on the domain instance for an ‘error’ event.
• Next, we are calling run method of domain2nd domain instance to demonstrate the explicit binding in the domain module.

Output
When we execute the above Node.js programs, then we will observe the following output on the console.

The listener will handle this error (It is to be handled by the listener)
domain1 handled this error (It is to be handled by the 1st domain)
events.js:141
      throw er; // Unhandled 'error' event
      ^

Error: It is converted to exception. Therefore the system will crash.
    at Object.<anonymous> (C:\odesk\Abhishek Thakur\NodeJS\DomainModule\DomainModule.js:19:26)
    at Module._compile (module.js:409:26)
    at Object.Module._extensions..js (module.js:416:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Function.Module.runMain (module.js:441:10)
    at startup (node.js:139:18)
    at node.js:968:3

Source code for the Domain Module Utility Module in Nodejs

Conclusion: –
In this article, we discussed various methods present in the Domain module of Node.js. We have imported the Domain module and use the associated methods to build a Domain Module utility.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Exclusive content

- Advertisement -

Latest article

21,501FansLike
4,106FollowersFollow
106,000SubscribersSubscribe

More article

- Advertisement -