Node.js 教程
- 第1节:Node.js 教程
- 第2节:Node.js 安装配置
- 第3节:Node.js 创建第一个应用
- 第4节:Node.js NPM 使用介绍
- 第5节:Node.js REPL(交互式解释器)
- 第6节:Node.js 回调函数
- 第7节:Node.js 事件循环
- 第8节:Node.js EventEmitter
- 第9节:Node.js Buffer(缓冲区)
- 第10节:Node.js Stream(流)
- 第11节:Node.js 模块系统
- 第12节:Node.js 函数
- 第13节:Node.js 路由
- 第14节:Node.js 全局对象
- 第15节:Node.js 常用工具util
- 第16节:Node.js 文件系统
- 第17节:Node.js GET/POST请求
- 第18节:Node.js 工具模块
- 第19节:Node.js Web 模块
- 第20节:Node.js Express 框架
- 第21节:Node.js RESTful API
- 第22节:Node.js 多进程
- 第23节:Node.js JXcore 打包
- 第24节:Node.js 连接 MySQL
- 第25节:Node.js 连接 MongoDB
- 第26节:关于 Node.js
- 第27节:Node.js 示例
- 第28节:Node.js 断言测试
- 第29节:Node.js 事件
- 第30节:Node.js Punycode
- 第31节:Node.js Buffer
- 第32节:Node.js 文件系统
- 第33节:Node.js Query Strings
- 第34节:Node.js C/C++ 插件
- 第35节:Node.js 逐行读取
- 第36节:Node.js 全局对象
- 第37节:Node.js 子进程
- 第38节:Node.js REPL
- 第39节:Node.js HTTP
- 第40节:Node.js 集群
- 第41节:Node.js Smalloc
- 第42节:Node.js HTTPS
- 第43节:Node.js 控制台
- 第44节:Node.js 模块
- 第45节:Node.js 加密
- 第46节:Node.js 流
- 第47节:Node.js 网络
- 第48节:Node.js 调试器
- 第49节:Node.js 字符串解码器
- 第50节:Node.js 系统
- 第51节:Node.js DNS
- 第52节:Node.js 定时器
- 第53节:Node.js 路径
- 第54节:Node.js 域
- 第55节:Node.js TLS/SSL
- 第56节:Node.js 进程
- 第57节:Node.js TTY
- 第58节:Node.js UDP/Datagram
- 第59节:Node.js URL
- 第60节:Node.js 实用工具
- 第61节:Node.js 虚拟机
- 第62节:Node.js ZLIB
Node.js 调试器
稳定性: 3 - 稳定
V8提供了强大的调试工具,可以通过TCP protocol从外部访问。Node内置这个调试工具客户端。使用这个调试器的方法是,以debug
参数启动Node.js,将会出现提示,指示调试器成功启动:
% node debug myscript.js
< debugger listening on port 5858
connecting... ok
break in /home/indutny/Code/git/indutny/myscript.js:1
1 x = 5;
2 setTimeout(function () {
3 debugger;
debug>
Node的调试器不支持所有的命令,但是简单的步进和检查还是可以的。在代码里嵌入debugger;
,可以设置断点。
例:myscript.js
代码如下:
// myscript.js
x = 5;
setTimeout(function () {
debugger;
console.log("world");
}, 1000);
console.log("hello");
如果启动debugger,它会断在第四行:
% node debug myscript.js
< debugger listening on port 5858
connecting... ok
break in /home/indutny/Code/git/indutny/myscript.js:1
1 x = 5;
2 setTimeout(function () {
3 debugger;
debug> cont
< hello
break in /home/indutny/Code/git/indutny/myscript.js:3
1 x = 5;
2 setTimeout(function () {
3 debugger;
4 console.log("world");
5 }, 1000);
debug> next
break in /home/indutny/Code/git/indutny/myscript.js:4
2 setTimeout(function () {
3 debugger;
4 console.log("world");
5 }, 1000);
6 console.log("hello");
debug> repl
Press Ctrl + C to leave debug repl
> x
5
> 2+2
4
debug> next
< world
break in /home/indutny/Code/git/indutny/myscript.js:5
3 debugger;
4 console.log("world");
5 }, 1000);
6 console.log("hello");
7
debug> quit
%
repl
命令能执行远程代码;next
能步进到下一行。此外可以输入help
查看哪些命令可用。
监视器-Watchers
调试的时候可以查看表达式和变量。每个断点处,监视器都会显示上下文。
输入watch("my_expression")
开始监视表达式,watchers
显示活跃的监视器。输入unwatch("my_expression")
可以移除监视器。
命令参考-Commands reference
步进-Stepping
cont
,c
- 继续执行next
,n
- Step nextstep
,s
- Step inout
,o
- Step outpause
- 暂停 (类似开发工具的暂停按钮)
断点Breakpoints
setBreakpoint()
,sb()
- 当前行设置断点setBreakpoint(line)
,sb(line)
- 在指定行设置断点setBreakpoint('fn()')
,sb(...)
- 在函数里的第一行设置断点setBreakpoint('script.js', 1)
,sb(...)
- 在 script.js 第一行设置断点。clearBreakpoint
,cb(...)
- 清除断点
也可以在尚未加载的文件里设置断点:
% ./node debug test/fixtures/break-in-module/main.js
< debugger listening on port 5858
connecting to port 5858... ok
break in test/fixtures/break-in-module/main.js:1
1 var mod = require('./mod.js');
2 mod.hello();
3 mod.hello();
debug> setBreakpoint('mod.js', 23)
Warning: script 'mod.js' was not loaded yet.
1 var mod = require('./mod.js');
2 mod.hello();
3 mod.hello();
debug> c
break in test/fixtures/break-in-module/mod.js:23
21
22 exports.hello = function() {
23 return 'hello from module';
24 };
25
debug>
信息Info
backtrace
,bt
- 打印当前执行框架的backtracelist(5)
- 显示脚本代码的5行上下文(之前5行和之后5行)watch(expr)
- 监视列表里添加表达式unwatch(expr)
- 从监视列表里删除表达式watchers
- 显示所有的监视器和它们的值(每个断点都会自动列出)repl
- 在所调试的脚本的上下文中,打开调试器的repl
执行控制Execution control
run
- 运行脚本 (开始调试的时候自动运行)restart
- 重新运行脚本kill
- 杀死脚本
杂项Various
scripts
- 列出所有已经加载的脚本version
- 显示v8版本
高级应用Advanced Usage
V8调试器可以用两种方法启用和访问,--debug
命令启动调试,或向已经启动Node发送SIGUSR1
。
一旦一个进程进入调试模式,它可以被node调试器连接。调试器可以通过pid
或URI来连接。
node debug -p <pid>
- 通过pid
连接进程node debug <URI>
- 通过URI(比如localhost:5858)连接进程w。