Skip to content

Commit 3903d45

Browse files
committed
add debug part
1 parent 33e4636 commit 3903d45

File tree

248 files changed

+38006
-2
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

248 files changed

+38006
-2
lines changed

doc/day1_express.md

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -698,11 +698,69 @@ see [node 测试](https://github.com/nodeonly/nodejs-tutorial/blob/master/doc/da
698698

699699
### node debug
700700

701+
V8 提供了一个强大的调试器,可以通过 TCP 协议从外部访问。Nodejs提供了一个内建调试器来帮助开发者调试应用程序。想要开启调试器我们需要在代码中加入debugger标签,当Nodejs执行到debugger标签时会自动暂停(debugger标签相当于在代码中开启一个断点)。代码如下:
702+
703+
see `demo/day1/debug/app_debug1.js`
704+
705+
var express = require('express');
706+
var app = express();
707+
708+
app.get('/',function(req,res){
709+
debugger;
710+
res.send('hello,world');
711+
});
712+
713+
714+
app.listen(5005);
715+
716+
module.exports = app;
717+
718+
719+
执行命令:`node debug app_debug1.js` 就可以进入调试模式。
720+
721+
当然,首先需要在程序代码中手动添加中断debugger; , 这样当以调试模式运行时,程序会自动中断,然后等候你调试,就像GDB一样,可以用help命令查看自己都可以使用哪些调试命令。
722+
723+
```
724+
debug> help
725+
Commands: run (r), cont (c), next (n), step (s), out (o), backtrace (bt), setBreakpoint (sb), clearBreakpoint (cb),
726+
watch, unwatch, watchers, repl, restart, kill, list, scripts, breakOnException, breakpoints, version
727+
```
728+
729+
这里就和gdb等调试器一模一样了
730+
731+
注意,如果出现
732+
733+
< Failed to open socket on port 5858, waiting 1000 ms before retrying
734+
735+
请结束掉所有debug进程
736+
737+
ps -ef|grep debug-brk|awk '{print $2}'|xargs kill -9
738+
701739
### node-inspector
702740

703-
node-debug app.js
741+
上面这种方式稍微有些麻烦,我们写JS代码调试的时候一般都用FireBug或谷歌浏览器内置的调试工具,nodejs程序当然也可以这样子来调试,但是首先需要安装一个node-inspector的东西
742+
743+
node-inspector是通过websocket方式来转向debug输入输出的。因此,我们在调试前要先启动node-inspector来监听Nodejs的debug调试端口。
744+
745+
746+
这个需要用npm来安装,只需要执行下列语句:
747+
748+
npm install -g node-inspector
749+
750+
安装完成之后,通常可以直接这样启动在后台:
751+
752+
node-inspector &
753+
754+
默认会监听8080端口,当然,也可能通过使用--web-port参数来修改。然后,在执行node程序的时候,多加个参数:--debug-brk, 如下:
755+
756+
node --debug-brk app.js
704757

705-
然后会在浏览器里打开一个页面
758+
或者
759+
760+
node-debug app.js
761+
762+
控制台会返回“debugger listening on port 5858”, 现在打开浏览嚣,访问http://localhost:8080,这时候就会打开一个很像Chrome内置调试工具的界面,并且代码断点在第一行,下面就可以使用这个来调试了。
763+
706764

707765
![](./images/debug.png)
708766

@@ -711,6 +769,8 @@ see [node 测试](https://github.com/nodeonly/nodejs-tutorial/blob/master/doc/da
711769

712770
使用方法和chrome的inspect element调试web开发是一样的。
713771

772+
调试还是很方便的,而且可以异地调试。
773+
714774
## 部署实战
715775

716776
### 开发环境下

doc/demo/day1/debug/app_deubug1.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
var express = require('express');
2+
var app = express();
3+
4+
app.get('/',function(req,res){
5+
debugger;
6+
res.send('hello,world');
7+
});
8+
9+
10+
app.listen(5005);
11+
12+
module.exports = app;

0 commit comments

Comments
 (0)