Open In App

Explain the concept of Domain in Node.js

Introduction: The Node.js domain module is used to catch outstanding errors. Domains provide a way to handle multiple different input-output operations as a single group. If either the event emitter or the callback is registered in the domain and when an error event or throws an error,  the domain object is notified instead of losing the context of the error. The domain error handler is not a substitute for closing the process when an error occurs. The safest way to deal with a thrown error is to close it.  Bring down the process. It is recommended that a typical web server send an error response to the request that threw the error and allow it. Other members will finish at normal time and stop listening for new requests from this worker.

The unhandled error can be intercepted using internal binding or external binding.



Syntax: To use the domain module we need to import it into our project directory. The syntax will be as follows: 

const domain = require("domain");

Note: According to the Official Nodejs documentation this domain model is pending deprecation. Once the replacement API is complete, this module will be completely deprecated. Users who absolutely need to have the functionality provided by the domain can trust it for now but expect to move to another solution in the future. Now we shouldn’t try to write new code with them. 



The domain class of the domain module is used to provide active domain objects with routing errors and uncaught exceptions. This is a subclass of EventEmitter. To handle the error you catch, listen for the error event. It is created with the following syntax.

const domain = require("domain");
const createdDomain = domain.create();

Example 1: This example will demonstrating the domain in Node.js:




const EventEmitter = require("events").EventEmitter;
const domain = require("domain");
 
const emitterExplicit = new EventEmitter();
 
// Create a firstDomain for explicit binding
const firstDomain = domain.create();
 
firstDomain.on('error', function(err) {
   console.log("This error will be handled by
       using firstDomain ("+err.message+")");
});
 
// Explicit binding using add method
firstDomain.add(emitterExplicit);
 
emitterExplicit.on('error',function(err) {
   console.log("This error will be handled by
       using listener ("+err.message+")");
});
 
emitterExplicit.emit('error',new Error('listener will handle'));
emitterExplicit.removeAllListeners('error');
emitterExplicit.emit('error',new Error('firstDomain will handle'));
 
// Create secondDomain for implicit binding
const secondDomain = domain.create();
 
secondDomain.on('error', function(err) {
   console.log("This error will be handled by
       using secondDomain ("+err.message+")");
});
 
// Implicit binding using run method
secondDomain.run(function() {
   const emitterImplicit = new EventEmitter();
   emitterImplicit.emit('error',new Error('secondDomain
       will handle'));  
});
 
firstDomain.remove(emitterExplicit);
emitterExplicit.emit('error', new Error('Exception error
    message. The system may crash!'));

Steps to run the application: Write the below code in the terminal to run the server:

node index.js

Output:

 

Example 2: The server is created using the explicit binding method. You can use one domain for the HTTP server, or you can use a separate domain for each request. req, and res is also created in the scope of serverUsingDomain. However, it’s a good idea to first create a separate domain for each request and then add req and res to it.




const domain = require('domain');
const http = require('http');
const serverUsingDomain = domain.create();
 
serverUsingDomain.run(() => {
  http.createServer((req, res) => {
 
    /* The server is created in the scope of
    serverUsingDomain. Req, and res is also
    created in the scope of serverUsingDomain.
    However, it's a good idea to first create
    a separate domain for each request and
    then add req and res to it. */
    const reqdomain = domain.create();
    reqdomain.add(req);
    reqdomain.add(res);
    reqdomain.on('error', (error) => {
      console.error('Error', error, req.url);
      try {
        res.writeHead(500).end('Some Error occurred.');
      } catch (error2) {
        console.error('Error is sending 500', error2, req.url);
      }
    });
  }).listen(5000, () => {
   console.log("Server is running on PORT 5000");
  });
});

Steps to run the application: Write the below code in the terminal to run the server:

node index.js

Output:

 

Method Available in Domain Module: – Let’s see some methods available in the domain module of Node.js.


Article Tags :