Domain Module in Node.js: Error and Event Management

The node:domain module provides a way to handle multiple I/O operations as a single group, routing errors and uncaught exceptions to a domain object instead of crashing the process. However, this module is deprecated and should not be used in new applications. Developers who rely on it must plan to migrate to safer alternatives such as async_hooks or structured error handling.

Domain classerror handlingimplicit bindingexplicit bindingcluster integration

~3 min read • Updated Dec 27, 2025

1. مقدمه


ماژول Domain برای مدیریت خطاها در گروهی از عملیات‌های IO طراحی شده است. اگر یک EventEmitter یا Callback خطا ایجاد کند، به‌جای خروج ناگهانی برنامه، خطا به دامنهٔ فعال ارسال می‌شود.


2. هشدار مهم


مدیریت خطا با Domain جایگزین بستن ایمن برنامه نیست. بهترین راهکار در صورت بروز خطا، خاتمهٔ فرآیند یا مدیریت مناسب درخواست‌هاست. استفادهٔ نادرست از Domain می‌تواند باعث نشت منابع و وضعیت‌های غیرقابل پیش‌بینی شود.


3. نمونهٔ استفادهٔ نادرست


// XXX WARNING! BAD IDEA!
const d = require('node:domain').create();
d.on('error', (er) => {
  console.log(`error, but oh well ${er.message}`);
});
d.run(() => {
  require('node:http').createServer((req, res) => {
    handleRequest(req, res);
  }).listen(PORT);
});

4. نمونهٔ استفادهٔ بهتر با Cluster


ترکیب Domain با ماژول Cluster امکان مدیریت ایمن‌تر خطاها را فراهم می‌کند:


const cluster = require('node:cluster');
if (cluster.isPrimary) {
  cluster.fork();
  cluster.fork();
  cluster.on('disconnect', (worker) => {
    cluster.fork();
  });
} else {
  const domain = require('node:domain');
  const server = require('node:http').createServer((req, res) => {
    const d = domain.create();
    d.on('error', (er) => {
      res.statusCode = 500;
      res.end('Oops, there was a problem!\n');
      cluster.worker.disconnect();
    });
    d.add(req);
    d.add(res);
    d.run(() => {
      handleRequest(req, res);
    });
  });
}

5. ویژگی‌های Domain


  • domain.create(): ایجاد یک دامنهٔ جدید.
  • domain.add(emitter): افزودن EventEmitter یا Timer به دامنه.
  • domain.bind(callback): اتصال یک Callback به دامنه.
  • domain.intercept(callback): مشابه bind اما خطاهای ارسال‌شده به‌عنوان آرگومان اول را نیز مدیریت می‌کند.
  • domain.run(fn): اجرای تابع در دامنهٔ فعال.
  • domain.enter() و domain.exit(): مدیریت ورود و خروج از دامنه.
  • domain.remove(emitter): حذف یک EventEmitter یا Timer از دامنه.

6. Binding


Implicit binding: همهٔ EventEmitterهای جدید به‌طور خودکار به دامنهٔ فعال متصل می‌شوند.
Explicit binding: می‌توان EventEmitterها یا درخواست‌ها را به‌طور دستی به دامنهٔ خاصی متصل کرد.


7. Domain و Promiseها


از نسخهٔ 8.0.0 به بعد، Handlerهای Promise در دامنه‌ای اجرا می‌شوند که متد .then() یا .catch() در آن فراخوانی شده است. Domainها در مدیریت خطاهای Promise دخالت نمی‌کنند.


نتیجه‌گیری


ماژول Domain در Node.js ابزاری برای مدیریت خطاها در عملیات‌های IO است، اما به‌دلیل مشکلات ذاتی و خطر نشت منابع، منسوخ شده است. توسعه‌دهندگان باید به راهکارهای جایگزین مانند استفاده از async_hooks یا مدیریت خطا در سطح برنامه مهاجرت کنند.


Written & researched by Dr. Shahin Siami