nodejs学习笔记Node.js 调试命令

3.4  调试        47 

下面是一个简单的例子: 

$ node debug debug.js 

< debugger listening on port 5858 connecting... ok 

break in /home/byvoid/debug.js:11 var a = 1;

2 var b = 'world'; 

3 var c = function (x) { debug> n 

break in /home/byvoid/debug.js:21 var a = 1;

2 var b = 'world'; 

3 var c = function (x) { 

4    console.log('hello ' + x + a); debug> sb('debug.js', 4) 

1  var a = 1; 

2  var b = 'world'; 

3  var c = function (x) { 

*  4   console.log('hello ' + x + a); 

5  }; 

6  c(b); 

7  }); 

debug> c 

break in /home/byvoid/debug.js:42 var b = 'world';

3 var c = function (x) { 

* 4   console.log('hello ' + x + a); 

5  }; 

6  c(b); debug> repl 

Press Ctrl + C to leave debug repl 

>  x 

'world' > a + 1 2 

debug> c 

< hello world1 program terminated

 

 

3.4.2  远程调试 

V8 提供的调试功能是基于 TCP 协议的,因此 Node.js 可以轻松地实现远程调试。在命 令行下使用以下两个语句之一可以打开调试服务器: 

node

--debug[=port] script.js

10

node

--debug-brk[=port] script.js

 

48        第 3 章    Node.js 快速入门 

node --debug 命令选项可以启动调试服务器,默认情况下调试端口是 5858,也可以 使用 --debug=1234 指定调试端口为 1234。使用 --debug 选项运行脚本时,脚本会正常 执行,但不会暂停,在执行过程中调试客户端可以连接到调试服务器。如果要求脚本暂停执 行等待客户端连接,则应该使用 --debug-brk 选项。这时调试服务器在启动后会立刻暂停 执行脚本,等待调试客户端连接。 

当调试服务器启动以后,可以用命令行调试工具作为调试客户端连接,例如: 

//在一个终端中 

$ node --debug-brk debug.js 

debugger listening on port 5858 

//在另一个终端中 

$ node debug 127.0.0.1:5858 connecting... okdebug> n 

break in /home/byvoid/debug.js:21 var a = 1;2 var b = 'world'; 

3 var c = function (x) { 

4 console.log('hello ' + x + a); debug> 

事实上,当使用 node debug debug.js 命令调试时,只不过是用 Node.js 命令行工 具将以上两步工作自动完成而已。

posted @ 2017-02-16 11:46  慕容小凡  阅读(1716)  评论(0编辑  收藏  举报