Page 56 - Node.js开发指南
P. 56

3.4  调试    47


                   下面是一个简单的例子:                                                                        1

                   $ node debug debug.js
                   < debugger listening on port 5858
                   connecting... ok
                   break in /home/byvoid/debug.js:1                                                   2
                     1 var a = 1;
                     2 var b = 'world';
                     3 var c = function (x) {
                   debug> n
                   break in /home/byvoid/debug.js:2
                     1 var a = 1;                                                                     3
                     2 var b = 'world';
                     3 var c = function (x) {
                     4   console.log('hello ' + x + a);
                   debug> sb('debug.js', 4)
                                                                                                      4
                     1 var a = 1;
                     2 var b = 'world';
                     3 var c = function (x) {
                   * 4   console.log('hello ' + x + a);
                     5 };
                     6 c(b);                                                                          5
                     7 });
                   debug> c
                   break in /home/byvoid/debug.js:4
                     2 var b = 'world';
                     3 var c = function (x) {                                                         6
                   * 4   console.log('hello ' + x + a);
                     5 };
                     6 c(b);
                   debug> repl
                   Press Ctrl + C to leave debug repl
                   > x                                                                                7
                   'world'
                   > a + 1
                   2
                   debug> c
                   < hello world1                                                                     8
                   program terminated

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

                   node --debug[=port] script.js
                   node --debug-brk[=port] script.js                                                  10
   51   52   53   54   55   56   57   58   59   60   61