Learn How to Build Domain Module Utility Modules in NodeJS

0
3956
Domain Module

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

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

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.

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