技术控

    今日:94| 主题:49113
收藏本版 (1)
最新软件应用技术尽在掌握

[其他] Debugging With Node.js

[复制链接]
fpwx0sOl 发表于 2016-10-4 22:21:05
282 9

立即注册CoLaBug.com会员,免费获得投稿人的专业资料,享用更多功能,玩转个人品牌!

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
I feel that debugging is as crucial a part of the development cycle as any other. So it's always good practice to demystify the job of debugging, making it easier and less time-consuming, so that we can end work on time and reduce stress.
  Like the majority of languages out there, Node provides some excellent debugging tools which make defects in code easily found and fixed. I always advocate the usage of a debugger because personally I find using debuggers really eliminates the need for any guesswork and makes us better developers in general.
  This guide is for developers and administrators that work with Node already. It presumes a fundamental understanding of the language at a practical level.
  Using the Debugger

  Node.js includes a full-featured out-of-process debugging utility accessible via a simple    TCP-based protocoland built-in debugging client.  
  For example, to use the debugger to debug a file named    script.js, you can simply call node using the    debugflag as so:  
  1. $ node debug script.js
  2. < debugger listening on port 5858
  3. connecting... ok
  4. debug>
复制代码
Breakpoints

  Now that you have started a debugging session, anywhere in your script that you call    debuggerfrom will be a breakpoint for the debugger.   
  So, for example, let's add a debugger statement to the script.js:
  1. foo = 2;
  2. setTimeout(() => {
  3.   debugger;
  4.   console.log('bugger');
  5. }, 1000);
  6. console.log('de');
复制代码
Now if we run this script the debugger will be called on our breakpoint and we can control the script control via using the    contor    nextcommands (    cor    nfor short).  
  We can pause the script execution at any time by using    p.  
  1. $ node debug script.js
  2. < debugger listening on port 5858
  3. connecting... ok
  4. break in /home/tom/web/envatodebug/myscript.js:1
  5.   1 foo = 5;
  6.   2 setTimeout(() => {
  7.   3   debugger;
  8. debug> cont
  9. < de
  10. break in /home/tom/web/envatodebug/myscript.js:3
  11.   1 foo = 5;
  12.   2 setTimeout(() => {
  13.   3   debugger;
  14.   4   console.log('bugger');
  15.   5 }, 1000);
  16. debug> next
  17. break in /home/tom/web/envatodebug/myscript.js:4
  18.   2 setTimeout(() => {
  19.   3   debugger;
  20.   4   console.log('bugger');
  21.   5 }, 1000);
  22.   6 console.log('de');
  23. debug> next
  24. < bugger
  25. break in /home/tom/web/envatodebug/myscript.js:5
  26.   3   debugger;
  27.   4   console.log('bugger');
  28.   5 }, 1000);
  29.   6 console.log('de');
  30.   7
  31. debug> quit
复制代码
REPL

  1. $ node debug script.js
  2. < debugger listening on port 5858
  3. connecting... ok
  4. debug> repl
  5. Press Ctrl + C to leave debug repl
  6. > foo
  7. 2
  8. > 2+2
  9. 4
复制代码
The Read-Eval-Print-Loop of the debugger allows you to enter code interactively during execution and thus access the state of the application and all of its variables and methods at the point of breaking execution. This is a very powerful tool which you can use to quickly sanitize your app.
  In general, the REPL is available as a standalone and as part of the debugger, and it allows you to run JavaScript interactively. For example, just type    nodeat the prompt with no options, and you will be given a REPL interface that you can write code into and see the output.  
  Stepping In & Stepping Out

  Earlier I mentioned the    contand     next(    cand    n) commands, which allow us to continue code execution once a breakpoint has been reached. In addition to this, as we walk through the code we can also step in to a method or step out to its parent scope.  
  Use the commands    stepto step in and    outto step out, or    sand    ofor short.  
  Backtracing

  Use    backtraceor    btto get an output of the backtrace for the current execution frame.  
  Restarting

  Use    restartor    rto restart your script from the beginning of execution.  
  Alternative Ways to Connect to the Debugger

  Advanced users can access the debugger also by starting Node.js with the    --debugcommand-line flag, or alternatively by signaling an existing Node.js process with     SIGUSR1.  
  Once a process has been set into the debug mode this way, it can then be connected to by using the Node.js debugger by either using the    pidof the running process or via a URI reference (e.g    localhost:port) to connect the listening debugger:  
  
       
  •       node debug -p <pid> connects to the process via the       pid.   
  •       node debug <URI> connects to the process via the URI such as      localhost:5858.  
  Using Node Inspector

  In addition to the CLI debug tool,    Node Inspectoralso provides a GUI inspector inside the web browser (currently only supporting Chrome and Opera).  
  To use the debugger, simply install as so:
  npm install -g node-inspector
  Now that we have the Node inspector installed, we can debug our script.js with:
  node-debug script.js
  Your shell will now output the following, and probably open the web browser to the URL if you have Chrome or Opera set as your default on your development OS.
  1. Node Inspector is now available from http://127.0.0.1:8080/?ws=127.0.0.1:8080&port=5858
  2. Debugging `script.js`  

  3. Debugger listening on port 5858
复制代码
In your web browser, you will now be able to debug your application in a similar environment to the developer tools package. Setting breakpoints and viewing code is now integrated with your browser view. Enjoy!
  Conclusion

  Debugging doesn't need to be a nightmare, nor does it need to be stressful.
  Setting breakpoints and stepping through code is so simple in Node. It's a very similar experience to Ruby, and if you are trying to understand an application you have been given, opening the app in debug mode and pausing execution is a fantastic way to learn in a rapid timeframe.
友荐云推荐




上一篇:深入解析DC/OS 1.8 – 高可靠的微服务及大数据管理平台
下一篇:Recap: Angular 2 Road Trip – Ahmedabad
酷辣虫提示酷辣虫禁止发表任何与中华人民共和国法律有抵触的内容!所有内容由用户发布,并不代表酷辣虫的观点,酷辣虫无法对用户发布内容真实性提供任何的保证,请自行验证并承担风险与后果。如您有版权、违规等问题,请通过"联系我们"或"违规举报"告知我们处理。

瞳哖旳糖果箼 发表于 2016-10-25 00:18:12
抢前排,坐下看看
回复 支持 反对

使用道具 举报

风速2010 发表于 2016-10-25 02:22:27
前排,坐等,支持fpwx0sOl,直播无敌,千秋万世
回复 支持 反对

使用道具 举报

邻家大叔 发表于 2016-10-29 14:25:11
有钱,就是任性,没钱,认命!
回复 支持 反对

使用道具 举报

晓旋 发表于 2016-11-7 20:47:53
坐等楼主现身!
回复 支持 反对

使用道具 举报

delihuaf 发表于 2016-11-9 07:26:23
顶贴是一种态度!
回复 支持 反对

使用道具 举报

了;可; 发表于 2016-11-12 23:19:49
发发呆,回回帖,工作结束~
回复 支持 反对

使用道具 举报

杜浩 发表于 2016-11-14 20:55:21
fpwx0sOl发几张靓照啊!
回复 支持 反对

使用道具 举报

黄永滨 发表于 2016-11-16 09:33:47
一觉醒来,天都黑了。
回复 支持 反对

使用道具 举报

wqrtx 发表于 2016-11-17 13:15:16
楼下各位,今年发大财!
回复 支持 反对

使用道具 举报

*滑动验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

我要投稿

推荐阅读

扫码访问 @iTTTTT瑞翔 的微博
回页顶回复上一篇下一篇回列表手机版
手机版/CoLaBug.com ( 粤ICP备05003221号 | 文网文[2010]257号 )|网站地图 酷辣虫

© 2001-2016 Comsenz Inc. Design: Dean. DiscuzFans.

返回顶部 返回列表