Node.js v8.x 中文文档
目录
Inspector#
The inspector
module provides an API for interacting with the V8 inspector.
It can be accessed using:
const inspector = require('inspector');
inspector.open([port[, host[, wait]]])#
Activate inspector on host and port. Equivalent to node--inspect=[[host:]port]
, but can be done programmatically after node hasstarted.
If wait is true
, will block until a client has connected to the inspect portand flow control has been passed to the debugger client.
inspector.close()#
Deactivate the inspector. Blocks until there are no active connections.
inspector.url()#
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 inspectorback-end and receiving message responses and notifications.
Constructor: new inspector.Session()#
Create a new instance of the inspector.Session
class. The inspector sessionneeds to be connected through session.connect()
before the messagescan be dispatched to the inspector backend.
inspector.Session
is an EventEmitter
with the following events:
Event: 'inspectorNotification'#
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: #
Emitted when an inspector notification is received that has its method field setto the
value.
The following snippet installs a listener on the Debugger.paused
event, and prints the reason for program suspension whenever programexecution 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 thrownif there is already a connected session established either through the API or bya front-end connected to the Inspector WebSocket port.
session.post(method[, params][, callback])#
Posts a message to the inspector back-end. callback
will be notified whena response is received. callback
is a function that accepts two optionalarguments - 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 theChrome DevTools Protocol Viewer.
Node inspector supports all the Chrome DevTools Protocol domains declaredby V8. Chrome DevTools Protocol domain provides an interface for interactingwith one of the runtime agents used to inspect the application state and listento the run-time events.
session.disconnect()#
Immediately close the session. All pending message callbacks will be calledwith an error. session.connect()
will need to be called to be able to sendmessages again. Reconnected session will lose all inspector state, such asenabled agents or configured breakpoints.