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 命令行工 具将以上两步工作自动完成而已。

最新文章

  1. OpenGL: 纹理采样 texture sample
  2. libuv源码分析前言
  3. C++: virtual inheritance and Cross Delegation
  4. EasyUI中在表单提交之前进行验证
  5. ios delegate, block, NSNotification用法
  6. OWIN的理解和实践(二) – Host和Server的开发
  7. Android ListView 自定义 Adapter
  8. 等额本息Vs等额本金
  9. Shell教程2-变量
  10. thinkphp action.class.php 学习
  11. JQ与AJAX 省市区三级联动下拉框
  12. iOS WebViewJavascriptBridge初步尝试与图文详细讲解
  13. 【转】常用Maven插件
  14. 「mysql优化专题」这大概是一篇最好的mysql优化入门文章(1)
  15. Delphi 打印 Tprinter
  16. Atcoder Beginner Contest 070 D - Transit Tree Path
  17. gitlab 之 cicd
  18. 服务器-华为RH5885 V3-安装Windows Server 2008R2后设备管理器中存在大量的感叹号,并且无法识别网络适配器,没有网卡
  19. python之参数解包
  20. LeetCode14.最长公共前缀

热门文章

  1. jquery widgets 弹框
  2. openal资料转贴
  3. 弹窗插件zDialog使用教程
  4. sourcetree管理git
  5. [转]ASP.NET MVC 5 - 给电影表和模型添加新字段
  6. shell基础篇(二)-shell变量
  7. 虚拟机 搭建LVS + DR + keepalived 高可用负载均衡
  8. jquery ajax返回html乱码解决
  9. 为非ajax请求绑定回调函数的方法
  10. Module &#39;null&#39; not found异常解决办法