Latest web development tutorials

Node.js Domain module

Node.js tool module Node.js tool module

Node.js Domain (domain)to simplify asynchronous exception handling code, you can catch exception handling try catch can not capture. The introduction of Domain module syntax is as follows:

var domain = require("domain")

domain module to handle a number of different IO operations as a group. Registration events and callbacks to domain, when an error event occurs or throw an error, domain objects will be notified, without losing the context, nor lead to procedural errors immediately launched, and process.on ( 'uncaughtException') are different.

Domain module can be divided into implicit and explicit binding Binding:

  • Implicit binding: the variable domain defined in the context, automatically bound to the domain objects
  • Explicit binding: the variable is not defined in the domain context, code-bound manner to the domain objects

method

No. Method & description
1 domain.run (function)
In the context of run the gamut of functions provided implicitly binding all of the event dispatcher, timers, and the underlying request.
2 domain.add (emitter)
Explicit increase event
3 domain.remove (emitter)
Delete event.
4 domain.bind (callback)
Function returns is provided for a callback wrapper functions. When the function call is returned, all thrown errors will be directed to the error event of this field.
5 domain.intercept (callback)
And domain.bind (callback) is similar. In addition to capturing error is thrown outside, it will intercept the Error object is passed to this function as a parameter.
6 domain.enter ()
Enter the context of an asynchronous call, bound to the domain.
7 domain.exit ()
Exit the current domain, context switch to a different chain of asynchronous call. Corresponds domain.enter ().
8 domain.dispose ()
The release of a domain object, and make this part of the recovery process node resources.
9 domain.create ()
Returns a domain object.

Attributes

No. Property & Description
1 domain.members
An array of domain timers and event emitter has joined domain object.

Examples

Create main.js file, the code is as follows:

var EventEmitter = require("events").EventEmitter;
var domain = require("domain");

var emitter1 = new EventEmitter();

// 创建域
var domain1 = domain.create();

domain1.on('error', function(err){
   console.log("domain1 处理这个错误 ("+err.message+")");
});

// 显式绑定
domain1.add(emitter1);

emitter1.on('error',function(err){
   console.log("监听器处理此错误 ("+err.message+")");
});

emitter1.emit('error',new Error('通过监听器来处理'));

emitter1.removeAllListeners('error');

emitter1.emit('error',new Error('通过 domain1 处理'));

var domain2 = domain.create();

domain2.on('error', function(err){
   console.log("domain2 处理这个错误 ("+err.message+")");
});

// 隐式绑定
domain2.run(function(){
   var emitter2 = new EventEmitter();
   emitter2.emit('error',new Error('通过 domain2 处理'));   
});


domain1.remove(emitter1);
emitter1.emit('error', new Error('转换为异常,系统将崩溃!'));

The above code is executed, the results are as follows:

监听器处理此错误 (通过监听器来处理)
domain1 处理这个错误 (通过 domain1 处理)
domain2 处理这个错误 (通过 domain2 处理)

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: 转换为异常,系统将崩溃!
    at Object.<anonymous> (/www/node/main.js:40:24)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:929:3

Node.js tool module Node.js tool module