Nodejs调试环境搭建

欲善其事,先利其器。Nodejs调试方法主要可以分为三类:

  • core node.js debugger:无用户接口,简陋,一般不用
  • 基于chome的node-inspector
  • IDE(如webstorm、phpsotrm)插件

以一个小demo(hello.js)为例对后面两者做介绍:

var http = require('http');
var url = require('url');

http
.createServer(function (req, res) {
    var path = url.parse(req.url).pathname;
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end(path);
})
.listen(1337, "127.0.0.1");

console.log('Server running at http://127.0.0.1:1337/');

Node-inspector

  1. npm安装node-inspector包:
    npm install -g node-inspector
  2. 后台进程的方式启动:
node-inspector &

注意:这样默认是以8080端口启动,该端口经常被apache、nginx等常用web服务占用而报错,错误内容如下:

➜  demo1 Node Inspector v0.12.8
Cannot start the server at 0.0.0.0:8080. Error: listen EADDRINUSE 0.0.0.0:8080.
There is another process already listening at this address.
Run `node-inspector --web-port={port}` to use a different port.

[1]  + 27585 done       node-inspector

这时可以以–web-port=[port]的方式指定其他端口,方式为:

node-inspector & --web-port=8008
  1. 启动node运行程序:
node --debug hello.js 

或者

node --debug-brk hello.js 
  1. 浏览器窗口输入如下
http://127.0.0.1:8008/?port=5858
  1. u will get hello.js in chrome window

大致说明

  1. –debug & –debug-brk的区别:
    –debug会运行在请求文件后立即用node执行该文件;
    –debug-brk会在文件第一行设中断,是否继续执行需要依据用户在chrome下的调试命令;
  2. chrome监听端口8008请求后,通知node-inspector(5858端口:可通过–debug-port[port]进行配置),最终基于NodeJS 提供的内建调试模块进行调试

参考链接:NodeJS的代码调试和性能调优

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值