NodeJS的调试工具

NodeJS常用调试工具包括两种:
1.NodeJS内置命令行调试工具
2.chrome浏览器页面调试
[size=large][b]NodeJS内置命令行调试[/b][/size]
var http = require('http');
var url = require('url');
http.createServer(function (req, res) {
var path = url.parse(req.url).pathname;
debugger;
var a = "bbb";
var b = "ccc";
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end(path);
}).listen(1337, "127.0.0.1");
console.log('Server running at http://127.0.0.1:1337/');

(1)代码中添加debugger;
(2)启动node时添加参数 node debug http.js
浏览器访问http://127.0.0.1:1337/,后台程序随即进入调试状态,其中绿色位置表示,当前代码断在首行
[img]http://dl.iteye.com/upload/attachment/0076/9874/4e685fc7-c9b3-3106-a3cd-14880e98f83f.bmp[/img]

此时涉及如何查看变量及如何下一步的操作,输入 helper 主要包括命令:

Commands: run (r), cont (c), next (n), step (s), out (o), backtrace (bt), setBreakpoint (sb), clearBreakpoint (cb),
watch, unwatch, watchers, repl, restart, kill, list, scripts, breakpoints, version


详细解析,有中文说明的经常用到:

run(r)
cont(c) 继续运行到下一个断点
next(n) 下一行(不进入函数Move over)
step(s) 下一步(进入当前行的详细Move down)
out(o) 跑到return位置
backtrace(bt) 显示所有断点位置
setBreakpoint(sb) 设置断点,后加行号
clearBreakpoint(cb) 清除所有断点信息
watch
unwatch
watchers
repl 进行repl模式,在这个模式查,可以直接查看变量的名称与及变量值
restart
kill
list 查看当前的代码及断点列表
scripts
breakpoints
version 查看调试器的版本

[size=large][b]chrome浏览器页面调试[/b][/size]
安装node-inspector: npm install -g node-inspector // -g 意思是安装到全局路径,全局路径查看方式 node -ls
启动node-inspector:
node-inspector & 默认监听的是5858端口
[img]http://dl.iteye.com/upload/attachment/0076/9944/55a82d51-c8fd-370b-b97a-d9eb158f7bb0.jpg[/img]
端口号默认为8080,如果与tomcat等冲突,在起inspector的时候可以指定端口,如:
node-inspector --web-port=8987
调试页面:http://127.0.0.1:8987/debug?port=5858
启动应用程序:
[img]http://dl.iteye.com/upload/attachment/0076/9950/9042914e-a1b5-30d4-b62a-4f0d4ab00cc5.png[/img]
访问调试页面:[url=http://127.0.0.1:8080/debug?port=5858]http://127.0.0.1:8080/debug?port=5858[/url]
即可在浏览器上进行调试,非常熟悉的前端界面吧 :D
[img]http://dl.iteye.com/upload/attachment/0076/9948/e05e8597-d164-3d7b-b9a3-6241799a249b.png[/img]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值