標籤:技術 rip connect nbsp home 開啟 用戶端 快速入門 bottom
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 命令列工 具將以上兩步工作自動完成而已。
nodejs學習筆記Node.js 調試命令