- assert - 断言
- Buffer - 缓冲器
- child_process - 子进程
- cluster - 集群
- console - 控制台
- crypto - 加密
- dgram - 数据报
- dns - 域名服务器
- Error - 异常
- events - 事件
- fs - 文件系统
- global - 全局变量
- http - HTTP
- https - HTTPS
- module - 模块
- net - 网络
- os - 操作系统
- path - 路径
- process - 进程
- querystring - 查询字符串
- readline - 逐行读取
- repl - 交互式解释器
- stream - 流
- string_decoder - 字符串解码器
- timer - 定时器
- tls - 安全传输层
- tty - 终端
- url - 网址
- util - 实用工具
- v8 - V8引擎
- vm - 虚拟机
- zlib - 压缩
Node.js v10.8.0 文档
Inspector#
本段落不提供翻译
The inspector
module provides an API for interacting with the V8 inspector.
It can be accessed using:
const inspector = require('inspector');
inspector.close()#
本段落不提供翻译
Deactivate the inspector. Blocks until there are no active connections.
inspector.console#
本段落不提供翻译
- <Object> An object to send messages to the remote inspector console.
require('inspector').console.log('a message');
The inspector console does not have API parity with Node.js console.
inspector.open([port[, host[, wait]]])#
本段落不提供翻译
port
<number> Port to listen on for inspector connections. Optional. Default: what was specified on the CLI.host
<string> Host to listen on for inspector connections. Optional. Default: what was specified on the CLI.wait
<boolean> Block until a client has connected. Optional. Default:false
.
Activate inspector on host and port. Equivalent to node --inspect=[[host:]port]
, but can be done programmatically after node has
started.
If wait is true
, will block until a client has connected to the inspect port
and flow control has been passed to the debugger client.
inspector.url()#
本段落不提供翻译
- Returns: <string> | <undefined>
Return the URL of the active inspector, or undefined
if there is none.
Class: inspector.Session#
本段落不提供翻译
The inspector.Session
is used for dispatching messages to the V8 inspector
back-end and receiving message responses and notifications.
Constructor: new inspector.Session()#
本段落不提供翻译
Create a new instance of the inspector.Session
class. The inspector session
needs to be connected through session.connect()
before the messages
can be dispatched to the inspector backend.
inspector.Session
is an EventEmitter
with the following events:
Event: 'inspectorNotification'#
本段落不提供翻译
- <Object> The notification message object
Emitted when any notification from the V8 Inspector is received.
session.on('inspectorNotification', (message) => console.log(message.method));
// Debugger.paused
// Debugger.resumed
It is also possible to subscribe only to notifications with specific method:
Event: <inspector-protocol-method>#
本段落不提供翻译
- <Object> The notification message object
Emitted when an inspector notification is received that has its method field set
to the <inspector-protocol-method>
value.
The following snippet installs a listener on the 'Debugger.paused'
event, and prints the reason for program suspension whenever program
execution is suspended (through breakpoints, for example):
session.on('Debugger.paused', ({ params }) => {
console.log(params.hitBreakpoints);
});
// [ '/the/file/that/has/the/breakpoint.js:11:0' ]
session.connect()#
本段落不提供翻译
Connects a session to the inspector back-end. An exception will be thrown if there is already a connected session established either through the API or by a front-end connected to the Inspector WebSocket port.
session.disconnect()#
本段落不提供翻译
Immediately close the session. All pending message callbacks will be called
with an error. session.connect()
will need to be called to be able to send
messages again. Reconnected session will lose all inspector state, such as
enabled agents or configured breakpoints.
session.post(method[, params][, callback])#
本段落不提供翻译
method
<string>params
<Object>callback
<Function>
Posts a message to the inspector back-end. callback
will be notified when
a response is received. callback
is a function that accepts two optional
arguments - error and message-specific result.
session.post('Runtime.evaluate', { expression: '2 + 2' },
(error, { result }) => console.log(result));
// Output: { type: 'number', value: 4, description: '4' }
The latest version of the V8 inspector protocol is published on the Chrome DevTools Protocol Viewer.
Node inspector supports all the Chrome DevTools Protocol domains declared by V8. Chrome DevTools Protocol domain provides an interface for interacting with one of the runtime agents used to inspect the application state and listen to the run-time events.
Example usage#
CPU Profiler#
本段落不提供翻译
Apart from the debugger, various V8 Profilers are available through the DevTools protocol. Here's a simple example showing how to use the CPU profiler:
const inspector = require('inspector');
const fs = require('fs');
const session = new inspector.Session();
session.connect();
session.post('Profiler.enable', () => {
session.post('Profiler.start', () => {
// invoke business logic under measurement here...
// some time later...
session.post('Profiler.stop', (err, { profile }) => {
// write profile to disk, upload, etc.
if (!err) {
fs.writeFileSync('./profile.cpuprofile', JSON.stringify(profile));
}
});
});
});