node.js之調試器 1.在命令行視窗中,可以使用"node debug" 命令來啟用調試器,代碼如下: node debug 接下來根據一個實例進行學習調試過程: 編寫app.js文件進行調試: console.log('hello,word') function foo(){ console. ...
node.js之調試器
1.在命令行視窗中,可以使用"node debug" 命令來啟用調試器,代碼如下:
node debug<需要被執行的腳本文件名>
接下來根據一個實例進行學習調試過程:
編寫app.js文件進行調試:
console.log('hello,word')
function foo(){
console.log('hello,foo')
return 100;
}
var bar = 'This is a pen';
var http = require('http')
var i = foo();
console.log(i)
1 在命令行視窗使用"node debug"命令,結果如下所示:
[root@kuber2 webproject]# node debug app.js
< Debugger listening on [::]:5858
connecting to 127.0.0.1:5858 ... ok
break in app.js:1
> 1 console.log('hello,word')
2 function foo(){
3 console.log('hello,foo')
debug>
在Node.js中提供了一個可以通過簡單TCP協議來訪問的調試器,進入調試後,首先連接調試器,如上圖所示出現Debugger並後面跟有ok,說明調試器已經成功使用。"break in app.js:1" 表示腳本程式暫停的位置
2繼續執行可輸入"cont"或"c",繼續執行剩餘的代碼,結果如下所示:
[root@kuber2 webproject]# node debug app.js
< Debugger listening on [::]:5858
connecting to 127.0.0.1:5858 ... ok
break in app.js:1
> 1 console.log('hello,word')
2 function foo(){
3 console.log('hello,foo')
debug> c
< hello,word
< hello,foo
< 100
3如果不需要執行剩餘的所有腳本代碼,輸入"next"或"n"將程式執行到下一句可執行代碼前,如下所示:
[root@kuber2 webproject]# node debug app.js
< Debugger listening on [::]:5858
connecting to 127.0.0.1:5858 ... ok
break in app.js:1
> 1 console.log('hello,word')
2 function foo(){
3 console.log('hello,foo')
debug> n
< hello,word
break in app.js:6
4 return 100;
5 }
> 6 var bar = 'This is a pen';
7 var http = require('http')
8 var i = foo();
debug>
4如果想進入函數進行調試,可以在函數被調用出輸入命令"step"或"s",進入函數體,如圖所示:
break in app.js:8
6 var bar = 'This is a pen';
7 var http = require('http')
> 8 var i = foo();
9 console.log(i)
10
debug> s
break in app.js:3
1 console.log('hello,word')
2 function foo(){
> 3 console.log('hello,foo')
4 return 100;
5 }
debug>
5在函數體中可以用"n",逐步進行調試,還可輸入"out"或"o" 立即執行完函數內所有代碼,程式將暫停到被調用函數代碼後的一句代碼前,如圖所示:
break in app.js:3
1 console.log('hello,word')
2 function foo(){
> 3 console.log('hello,foo')
4 return 100;
5 }
debug> n
< hello,foo
break in app.js:4
2 function foo(){
3 console.log('hello,foo')
> 4 return 100;
5 }
6 var bar = 'This is a pen';
debug> o
break in app.js:9
7 var http = require('http')
8 var i = foo();
> 9 console.log(i)
10
11 });
debug>
6 利用watch可以觀察變數或一些表達式的值,利用unwatch可以取消觀看,具體應用如下代碼所示:
break in app.js:1
> 1 console.log('hello,word')
2 function foo(){
3 console.log('hello,foo')
debug> watch("i")
debug> watch("i==100")
debug> n
< hello,word
break in app.js:6
Watchers:
0: i = undefined
1: i==100 = false
4 return 100;
5 }
> 6 var bar = 'This is a pen';
7 var http = require('http')
8 var i = foo();
debug> n
break in app.js:7
Watchers:
0: i = undefined
1: i==100 = false
5 }
6 var bar = 'This is a pen';
> 7 var http = require('http')
8 var i = foo();
9 console.log(i)
debug> n
break in app.js:8
Watchers:
0: i = undefined
1: i==100 = false
6 var bar = 'This is a pen';
7 var http = require('http')
> 8 var i = foo();
9 console.log(i)
10
debug> n
< hello,foo
break in app.js:9
Watchers:
0: i = 100
1: i==100 = true
7 var http = require('http')
8 var i = foo();
> 9 console.log(i)
10
11 });
debug> unwatch("i")
debug> unwatch("i==100")
debug> n
< 100
break in app.js:11
9 console.log(i)
10
>11 });
debug>