一、基于nodejs内建的调试

node.js本身支持调试,在js文件中加上debbuger或者在cmd中通过setBreakpoint()可以添加断点。

要使用nodejs的调试服务,只要在启动命令时加上debug。如:node debug web.js。

nodeJS教程(二)、nodejs调试-LMLPHP

这时候输入一些指令就能够单步调试、监视变量。

比如web.js文件如下:

var http = require("http");
http.createServer(function(request, response) {
    response.writeHead(200, {'Content-Type': 'text/plain'});
	response.write("111");
	debugger;
	response.end("222");
	debugger;
}).listen(8000);

在上面的命令行输入cont之后会监听服务。客户端打开http://127.0.0.1:8000/后,这时候会进入第一个断点(第6行)。输入repl,就能够查看当前上下文变量。退出repl后,输入cont可以进入下一个断点。

nodeJS教程(二)、nodejs调试-LMLPHP

二、内置node-inspect+chrome调试

全局安装node-inspect:npm install -g node-inspect

nodeJS教程(二)、nodejs调试-LMLPHP

cmd中输入:node --inspect-brk  文件名。(node --debug现在已经不推荐使用,改成--inspect了)

nodeJS教程(二)、nodejs调试-LMLPHP

chrome浏览器打开服务器url后,打开控制台。点击控制台的nodejs图标,就可以打开调试窗口。

nodeJS教程(二)、nodejs调试-LMLPHP

或者输入chrome://inspect/#devices,在打开的页面中,找到Remote Target下面的内容:

nodeJS教程(二)、nodejs调试-LMLPHP

调试效果如下:

nodeJS教程(二)、nodejs调试-LMLPHP

10-04 19:24