Node.js是什么?Node.js适合什么场景?多少公司已用于生产环境?Node.js靠不靠谱?这个话题应该无可厚非了,如果今天还在质疑Node.js的能力,相信它一定不是一家面向互联网公司。
  
自 2009 年Ryan Dahl发布后,到现在已经六个年头了。人红是非多,先后经历了ruby、php web擂台之战,后因某些原因派生出了iojs这个兄弟;分久必合,大势所趋。最近两兄弟又合体了,衍生出了Node.js V4.0版本,官方又解释这个版本是Node.js V1.0 新版本,原班人马重新回归。关于1.0新时代,有兴趣的童鞋请参考详情:http://mp.weixin.qq.com/s?__biz=MzAxMTU0NTc4Nw==&mid=239799758&idx=1&sn=9762fe70f14829e7bd42fb1f35a6362e&scene=23&srcid=1011W2vcX80qGPXQEhjJAeB5#rd。
  
btw,po一个js全栈图:
  ![[js全栈示例] [js全栈示例]](/2015/10/11/web-nodejs-debug/nodejs.png)
断言调试
我们日常使用IDE工具,实际上很多IDE工具已经集成了Node.js的调试工具;
  
比如Eclipse、webStorm等等,他们断言的原理是利用Node.js的Debugger内建模块,然后在这个基础上进行了封装。
Debugger内建模块
Node.js提供的内建Debugger模块十分强大,它告诉V8,在执行代码的时候中断程度,等待开发者操控代码的执行进度。
- 
    
立即调试模式:
一般用于程序主动执行方式,比如一个程序计算某种结果,并非要监听某个端口来等待请求。node debug xx.js// 利用命令行来向5858端口传送调试指令 - 
    
等待调试模式:
正好与立即模式相反,常用于web程序 e.g. express要调试,必须使用这种方式启动,然后当请求来了才能进行调试,并非程序马上执行的类型。node --debug xx.js// 监听5858默认端口接收调试命令node --debug-brk=8080 xx.js// 监听8080端口来接收调试指令 
立即调试模式
see test.js
  
  
      
                
         
                var hello = 'hello';
                
                
var world = 'nodejs';
                
debugger;
                
var hello_world = hello + ' ' + world;
                
console.log(hello_world);
                
                
执行命令:node debug test.js 就可以进入调试模式。
                  
                  
                      
                  
                         
                    ➜  $ node debug test.js
< debugger listening on port 5858
connecting... ok
break in helloword-debug.js:1
  1 var hello = 'hello';
  2 var world = 'nodejs';
  3
debug> help
Commands: run (r), cont (c), next (n), step (s), out (o), backtrace (bt), setBreakpoint (sb), clearBreakpoint (cb),
watch, unwatch, watchers, repl, restart, kill, list, scripts, breakOnException, breakpoints, version
debug>
debug> n
break in helloword-debug.js:2
  1 var hello = 'hello';
  2 var world = 'nodejs';
  3
  4 debugger;
debug> repl
Press Ctrl + C to leave debug repl
> hello
'hello'
等待调试模式
➜ $ node --debug test.js // 告知V8,test.js在默认端口5858上接收指令  | 
                    
当我们使用debug参数打开一个node文件时,会输出这样一行文字:
                  
                  
打开浏览器访问 http://localhost:5858:
                  ![[node --debug test.js] [node --debug test.js]](/2015/10/11/web-nodejs-debug/debug.png)
--debug其实是告诉用户Node在运行脚本时启动了内建debugger功能,并监听5858端口来处理传输的的调试命令。
                  
这里强调一下,如果要进行远程调试,还要结合node debug来实现:
- 
                    
服务端(程序):
➜ $ node --debug server.js // 通知V8开启调试功能,等待接收调试指令
 - 
                    
客户端(终端):
node debug <URI>, 通过 URI 连接调试,e.g. node debug <服务器IP>:<调试端口,默认5858>
node debug -p <pid> 通过 PID 链接调试
node debug localhost:5858
➜ $ node debug localhost:5858 // 客户端可以远程调试--debug启动server.js 
当然如果要修改默认端口,可以用选项--debug-brk=8080这样的方式:
                  
                  
                      
                  
                         
                    ➜  $ node --debug-brk=8080 xx.js // 通知V8开启调试功能,等待接收调试指令
推荐node-inspector神器
Node.js断言技术有很多种方式,本质都是基于Node.js本身提供的debugger原理。
                  
推荐用户使用node-inspector原因是它不依赖某个IDE,接近原生且更加灵活,并且可以断言coffee预处理js。
                  
所以下面来讲讲Node.js神器node-inspector。
                  node-inspector就是用了上述等待调试模式原理,另外加上websocket技术传输信息,并且提供GUI方便用户调试。
原理
node-inspector提供一个web server作为GUI接收用户的指令,然后内部用websocket协议与需要被调试的程序进行实时交互。
                  
                  
                      
                  
                         
                    用户指令 ---GUI--8080---> node-inspector ---websocket--5858---> node\'s debugger
实践
- 
                    
安装node-inspector:
➜ $ npm install -g node-inspector // 安装node-inspector
 - 
                    
启动node-inspector:
开启一个终端窗口,执行如下➜ $ node-inspector // 直接监听默认端口8080
➜ $ node-inspector --web-port 8080 --debug-port 5858--web-port是Chrome Devtools的调试页面端口,--debug-port为Node.js启动的内建debug端口,即需被调试的程序xx.jsnode --debug-brk=5858 xx.js等待接收调试指令的端口! 😄 - 
                    
启动被调试程序:
再开启一个终端窗口,执行如下➜ $ node --debug-brk=5858 test.js || node --debug test.js
或者
➜ $ node-debug test.js 
可以在 http://localhost:8080/debug?port=5858 打开node-inspector页面,调试使用--debug(-brk)或node-debug参数启动的程序。
                  
                  
更多设置可以查阅官方文档。
tips:这里提示一下利用node-inspector调试coffeescript程序时的设置,开启node-inspector方式一样,但启动需要被调试的程序有点不一样,如下。
                  
                  
                      
                  
                         
                    ➜  $ coffee --nodejs --debug xx.coffee
FAQ
注意,如果出现
                  
                  
                      
                  
                         
                    ➜  $ Failed to open socket on port 5858, waiting 1000 ms before retrying
请结束掉所有debug进程
                  
                  
                      
                  
                         
                    ➜  $ ps -ef|grep debug-brk|awk '{print $2}'|xargs kill -9
总结
只有真正理解了一门语言提供的debug技术的原理,才能更加熟练地使用它来调试程序。
参考:
                  
https://nodejs.org
                  
https://iojs.org