- assert 断言
- async_hooks 异步钩子
- async_hooks/context 异步上下文
- buffer 缓冲区
- C++插件
- C/C++插件(使用Node-API)
- C++嵌入器
- child_process 子进程
- cluster 集群
- CLI 命令行
- console 控制台
- Corepack 核心包
- crypto 加密
- crypto/webcrypto 网络加密
- debugger 调试器
- deprecation 弃用
- dgram 数据报
- diagnostics_channel 诊断通道
- dns 域名服务器
- domain 域
- Error 错误
- events 事件触发器
- fs 文件系统
- global 全局变量
- http 超文本传输协议
- http2 超文本传输协议2.0
- https 安全超文本传输协议
- inspector 检查器
- Intl 国际化
- module 模块
- module/cjs CommonJS模块
- module/esm ECMAScript模块
- module/package 包模块
- net 网络
- os 操作系统
- path 路径
- perf_hooks 性能钩子
- permission 权限
- process 进程
- punycode 域名代码
- querystring 查询字符串
- readline 逐行读取
- repl 交互式解释器
- report 诊断报告
- sea 单个可执行应用程序
- stream 流
- stream/web 网络流
- string_decoder 字符串解码器
- test 测试
- timers 定时器
- tls 安全传输层
- trace_events 跟踪事件
- tty 终端
- url 网址
- util 实用工具
- v8 引擎
- vm 虚拟机
- wasi 网络汇编系统接口
- worker_threads 工作线程
- zlib 压缩
Node.js v20.2.0 文档
- Node.js v20.2.0
- ► 目录
-
►
导航
- assert 断言
- async_hooks 异步钩子
- async_hooks/context 异步上下文
- buffer 缓冲区
- C++插件
- C/C++插件(使用Node-API)
- C++嵌入器
- child_process 子进程
- cluster 集群
- CLI 命令行
- console 控制台
- Corepack 核心包
- crypto 加密
- crypto/webcrypto 网络加密
- debugger 调试器
- deprecation 弃用
- dgram 数据报
- diagnostics_channel 诊断通道
- dns 域名服务器
- domain 域
- Error 错误
- events 事件触发器
- fs 文件系统
- global 全局变量
- http 超文本传输协议
- http2 超文本传输协议2.0
- https 安全超文本传输协议
- inspector 检查器
- Intl 国际化
- module 模块
- module/cjs CommonJS模块
- module/esm ECMAScript模块
- module/package 包模块
- net 网络
- os 操作系统
- path 路径
- perf_hooks 性能钩子
- permission 权限
- process 进程
- punycode 域名代码
- querystring 查询字符串
- readline 逐行读取
- repl 交互式解释器
- report 诊断报告
- sea 单个可执行应用程序
- stream 流
- stream/web 网络流
- string_decoder 字符串解码器
- test 测试
- timers 定时器
- tls 安全传输层
- trace_events 跟踪事件
- tty 终端
- url 网址
- util 实用工具
- v8 引擎
- vm 虚拟机
- wasi 网络汇编系统接口
- worker_threads 工作线程
- zlib 压缩
- ► 其他版本
- 云服务器
目录
域#
源代码: lib/domain.js
此模块正在等待弃用。 一旦替换 API 完成,则此模块将被完全弃用。 大多数开发者应该 没有有理由使用这个模块。 绝对必须拥有域提供的功能的用户可能暂时依赖它,但预计将来必须迁移到不同的解决方案。
域提供了一种将多个不同的 IO 操作作为一组来处理的方法。 如果任何注册到域的事件触发器或回调触发 'error'
事件,或抛出错误,则将通知域对象,而不是丢失 process.on('uncaughtException')
句柄中的错误上下文,或导致程序立即使用错误码退出。
警告:不要忽略错误!#
发生错误时,域错误句柄不能替代关闭进程。
根据 throw
在 JavaScript 中的工作方式的本质,几乎没有任何方法可以在不泄漏引用或创建某种其他类型的未定义的脆弱状态的情况下安全地 "从停止的地方继续"。
响应抛出的错误最安全的方法是关闭进程。 但是,在正常的 web 服务器中,可能有很多打开的连接,因为别人触发了错误而突然关闭这些连接是不合理的。
更好的方法是向触发错误的请求发送错误响应,同时让其他人在正常时间完成,并停止在该工作进程中监听新的请求。
通过这种方式,domain
的使用与集群模块齐头并进,因为当工作进程遇到错误时,主进程可以衍生新的工作进程。 对于扩展到多台机器的 Node.js 程序,终止的代理或服务仓库可以记录故障,并做出相应的反应。
例如,这不是一个好主意:
// XXX WARNING! BAD IDEA!
const d = require('node:domain').create();
d.on('error', (er) => {
// The error won't crash the process, but what it does is worse!
// Though we've prevented abrupt process restarting, we are leaking
// a lot of resources if this ever happens.
// This is no better than process.on('uncaughtException')!
console.log(`error, but oh well ${er.message}`);
});
d.run(() => {
require('node:http').createServer((req, res) => {
handleRequest(req, res);
}).listen(PORT);
});
通过使用域的上下文,以及将我们的程序分成多个工作进程的弹性,我们可以做出更适当的反应,并以更高的安全性处理错误。
// Much better!
const cluster = require('node:cluster');
const PORT = +process.env.PORT || 1337;
if (cluster.isPrimary) {
// A more realistic scenario would have more than 2 workers,
// and perhaps not put the primary and worker in the same file.
//
// It is also possible to get a bit fancier about logging, and
// implement whatever custom logic is needed to prevent DoS
// attacks and other bad behavior.
//
// See the options in the cluster documentation.
//
// The important thing is that the primary does very little,
// increasing our resilience to unexpected errors.
cluster.fork();
cluster.fork();
cluster.on('disconnect', (worker) => {
console.error('disconnect!');
cluster.fork();
});
} else {
// the worker
//
// This is where we put our bugs!
const domain = require('node:domain');
// See the cluster documentation for more details about using
// worker processes to serve requests. How it works, caveats, etc.
const server = require('node:http').createServer((req, res) => {
const d = domain.create();
d.on('error', (er) => {
console.error(`error ${er.stack}`);
// We're in dangerous territory!
// By definition, something unexpected occurred,
// which we probably didn't want.
// Anything can happen now! Be very careful!
try {
// Make sure we close down within 30 seconds
const killtimer = setTimeout(() => {
process.exit(1);
}, 30000);
// But don't keep the process open just for that!
killtimer.unref();
// Stop taking new requests.
server.close();
// Let the primary know we're dead. This will trigger a
// 'disconnect' in the cluster primary, and then it will fork
// a new worker.
cluster.worker.disconnect();
// Try to send an error to the request that triggered the problem
res.statusCode = 500;
res.setHeader('content-type', 'text/plain');
res.end('Oops, there was a problem!\n');
} catch (er2) {
// Oh well, not much we can do at this point.
console.error(`Error sending 500! ${er2.stack}`);
}
});
// Because req and res were created before this domain existed,
// we need to explicitly add them.
// See the explanation of implicit vs explicit binding below.
d.add(req);
d.add(res);
// Now run the handler function in the domain.
d.run(() => {
handleRequest(req, res);
});
});
server.listen(PORT);
}
// This part is not important. Just an example routing thing.
// Put fancy application logic here.
function handleRequest(req, res) {
switch (req.url) {
case '/error':
// We do some async stuff, and then...
setTimeout(() => {
// Whoops!
flerb.bark();
}, timeout);
break;
default:
res.end('ok');
}
}
Error
对象的添加#
每当 Error
对象通过域路由时,都会向其中添加一些额外的字段。
error.domain
最先处理错误的域。error.domainEmitter
触发带有错误对象的'error'
事件的事件触发器。error.domainBound
绑定到域的回调函数,并将错误作为其第一个参数传递。error.domainThrown
一个布尔值,指示错误是被抛出、触发还是传递给绑定的回调函数。
隐式绑定#
如果域正在使用中,则所有新的 EventEmitter
对象(包括流对象、请求、响应等)将在创建时隐式地绑定到活动域。
此外,传递给低级别事件循环请求(例如 fs.open()
或其他回调获取方法)的回调将自动绑定到活动域。 如果它们抛出,则域将捕获错误。
为了防止过多的内存使用,Domain
对象本身没有被隐式地添加为活动域的子域。 如果是这样,则阻止请求和响应对象被正确地垃圾收集就太容易了。
要将 Domain
对象嵌套为父 Domain
的子对象,则必须显式地添加它们。
隐式的绑定路由向 Domain
的 'error'
事件抛出错误和 'error'
事件,但没有在 Domain
上注册 EventEmitter
。
隐式的绑定只处理抛出的错误和 'error'
事件。
显式绑定#
有时,使用的域不是应该用于特定事件触发器的域。 或者,事件触发器可以在域的上下文中创建,但可以绑定到其他域。
例如,可能有一个域用于 HTTP 服务器,但也许我们希望为每个请求使用单独的域。
这可以通过显式绑定来实现。
// Create a top-level domain for the server
const domain = require('node:domain');
const http = require('node:http');
const serverDomain = domain.create();
serverDomain.run(() => {
// Server is created in the scope of serverDomain
http.createServer((req, res) => {
// Req and res are also created in the scope of serverDomain
// however, we'd prefer to have a separate domain for each request.
// create it first thing, and add req and res to it.
const reqd = domain.create();
reqd.add(req);
reqd.add(res);
reqd.on('error', (er) => {
console.error('Error', er, req.url);
try {
res.writeHead(500);
res.end('Error occurred, sorry.');
} catch (er2) {
console.error('Error sending 500', er2, req.url);
}
});
}).listen(1337);
});
domain.create()
#
- 返回: <Domain>
类:Domain
#
- 继承: <EventEmitter>
Domain
类封装了路由错误和未捕获异常到活动 Domain
对象的功能。
要处理其捕获的错误,则监听其 'error'
事件。
domain.members
#
已显式地添加到域的定时器和事件触发器数组。
domain.add(emitter)
#
emitter
<EventEmitter> | <Timer> 要添加到域中的触发器或定时器
显式地添加触发器到域中。 如果触发器调用的任何事件句柄抛出错误,或者触发器触发 'error'
事件,则它将被路由到域的 'error'
事件,就像隐式绑定一样。
这也适用于从 setInterval()
和 setTimeout()
返回的定时器。 如果其回调函数抛出异常,则其将被域 'error'
句柄捕获。
如果定时器或 EventEmitter
已绑定到某个域,则将其从该域中删除,并改为绑定到该域。
domain.bind(callback)
#
callback
<Function> 回调函数- 返回: <Function> 绑定的函数
返回的函数将是提供的回调函数的封装器。 当调用返回的函数时,抛出的任何错误都会被路由到域的 'error'
事件。
const d = domain.create();
function readSomeFile(filename, cb) {
fs.readFile(filename, 'utf8', d.bind((er, data) => {
// If this throws, it will also be passed to the domain.
return cb(er, data ? JSON.parse(data) : null);
}));
}
d.on('error', (er) => {
// An error occurred somewhere. If we throw it now, it will crash the program
// with the normal line number and stack message.
});
domain.enter()
#
enter()
方法是 run()
、bind()
和 intercept()
方法用来设置活动域的管道。 它将 domain.active
和 process.domain
设置为域,并将域隐式推送到域模块管理的域堆栈上(有关域堆栈的详细信息,请参见 domain.exit()
)。 对 enter()
的调用界定了一系列异步调用和绑定到域的 I/O 操作的开始。
调用 enter()
只改变活动域,不改变域本身。 enter()
和 exit()
可以在单个域上调用任意次数。
domain.exit()
#
exit()
方法退出当前域,将其从域堆栈中弹出。
任何时候执行将切换到不同异步调用链的上下文,确保退出当前域很重要。
对 exit()
的调用界定了异步调用链和绑定到域的 I/O 操作链的结束或中断。
如果有多个嵌套域绑定到当前执行上下文,则 exit()
将退出任何嵌套在该域中的域。
调用 exit()
只改变活动域,不改变域本身。 enter()
和 exit()
可以在单个域上调用任意次数。
domain.intercept(callback)
#
callback
<Function> 回调函数- 返回: <Function> 截获的函数
此方法和 domain.bind(callback)
差不多。 但是,除了捕获抛出的错误外,它还会拦截作为第一个参数发送给函数的 Error
对象。
这样,常见的 if (err) return callback(err);
模式可以在一个地方用单个错误句柄替换。
const d = domain.create();
function readSomeFile(filename, cb) {
fs.readFile(filename, 'utf8', d.intercept((data) => {
// Note, the first argument is never passed to the
// callback since it is assumed to be the 'Error' argument
// and thus intercepted by the domain.
// If this throws, it will also be passed to the domain
// so the error-handling logic can be moved to the 'error'
// event on the domain instead of being repeated throughout
// the program.
return cb(null, JSON.parse(data));
}));
}
d.on('error', (er) => {
// An error occurred somewhere. If we throw it now, it will crash the program
// with the normal line number and stack message.
});
domain.remove(emitter)
#
emitter
<EventEmitter> | <Timer> 要从域中删除的触发器或定时器
domain.add(emitter)
的反义词。 从指定的触发器中删除域处理。
domain.run(fn[, ...args])
#
fn
<Function>...args
<any>
在域上下文中运行提供的函数,隐式绑定在该上下文中创建的所有事件触发器、计时器和低级请求。 可选地,参数可以传给函数。
这是使用域的最基本方式。
const domain = require('node:domain');
const fs = require('node:fs');
const d = domain.create();
d.on('error', (er) => {
console.error('Caught error!', er);
});
d.run(() => {
process.nextTick(() => {
setTimeout(() => { // Simulating some various async stuff
fs.open('non-existent file', 'r', (er, fd) => {
if (er) throw er;
// proceed...
});
}, 100);
});
});
在本例中,将触发 d.on('error')
句柄,而不是使程序崩溃。
域和 promise#
从 Node.js 8.0.0 开始,promise 的句柄在调用 .then()
或 .catch()
本身的域内运行:
const d1 = domain.create();
const d2 = domain.create();
let p;
d1.run(() => {
p = Promise.resolve(42);
});
d2.run(() => {
p.then((v) => {
// running in d2
});
});
可以使用 domain.bind(callback)
将回调绑定到特定域:
const d1 = domain.create();
const d2 = domain.create();
let p;
d1.run(() => {
p = Promise.resolve(42);
});
d2.run(() => {
p.then(p.domain.bind((v) => {
// running in d1
}));
});
域不会干扰 promise 的错误处理机制。 换句话说,对于未处理的 Promise
拒绝,不会触发 'error'
事件。