<span style="font-family: Arial, Helvetica, sans-serif;">简单记录一下 debug 模块的使用,有些时候,我们用console.log打印了很多东西,到最终项目成型,可能需要去掉,会比较麻烦。</span>
使用debug工具则可以继续保留,想要打印的话,只需要进入debug状态就可以继续打印出想要的结果。
1、首先可能需要安装 debug模块
npm install debug
2、然后在代码中测试:
写一个脚本test.js
var debug = require('debug')('http')
, http = require('http')
, name = 'My App';
// fake app
debug('booting %s', name);
http.createServer(function(req, res){
debug(req.method + ' ' + req.url);
res.end('hello debug!\n');
}).listen(3000, function(){
debug('listening');
});
3、在debug环境下运行:
windows下:
set DEBUG=*,-not_this
然后 node test.js 可以看到结果
http booting My App +0ms
http listening +9ms
linux下:
DEBUG=* node test.js
然后可以看到结果:
http booting My App +0ms
http listening +9ms