日期:2014-05-16  浏览次数:20396 次

如何调试NodeJS

查看全文:http://ramonblog.cloudfoundry.com/blog/5002d26f4e1ef7b729000002


nodejs系列提纲

我终于要不能忍受CSDN这个垃圾网站的blog了,于是乎我决定借着学习nodejs的机会自己搭建一个blog,并且将其部署到cloudfoundry上。于是乎我将陆续完成下面的系列文章。所以文章里面的代码都是这个blog的实现。

  1. Why NodeJS
  2. nodejs的架构
  3. nodejs的hello world
  4. nodejs类库
  5. nodejs的plugin,如何实现一个plugin
  6. nodejs的web framework Expression
  7. nodejs与mongoDB
  8. nodejs与cloudfoundry
  9. nodejs如何调试(Node Inspector)


debug nodejs

Sure it's important to debug for any programming language, and it should be learnt at first time to use a new language. I forgot why I put this debug blog at the end of the serials blogs. No matter what's the reason, now we need to introduce how to debug nodejs. This article has two parts, one is the native debugger for nodejs, and another one is Node Inspector. In fact there is the third way to debug nodejs, based on Eclipse, which is not introduced here, but you can find more introduction from here: Using Eclipse as Node Applications Debugger.

微笑NodeJS Built-in debugger

  1. Prepare to debug Have below simple code as our sample, name it as 'debug.js'.

    var count = 10;
    var sum = 0;
    debugger;
    for (var i = 1; i < count; i++) {
        sum += i;
    }
    console.log(sum);
    

    Note we have debugger; at line 3, which means we put a breakpoint here. Actually it's the same with the normal Javascript running in browser Chrome, in which put a debugger; statement. The difference it we add this statement into js code running in browser to force the execution to pause at load time. But in to debug nodejs, it's not necessary, because in debug mode the execution is always paused at first line.

    。。。。。。


Node Inspector

Finally we finish the basic commands of built-in debugger. Now we introduce a debug tool with UI