调试有好多种方法:
命令行调试:
var name=carrot;
var x=welcome;
var c=function(name){
console.log('Hello '+name+x);
};
c(b);
在命令行下执行node debug debug.js
这样就打开了一个Node.js的调试终端,
可以使用一些命令进行单步跟踪调试:run(执行脚本,在第一行暂停),next n(单步执行) , step ,s(单步执行并进入函数)
远程调试:
node--debug[=port] script.js
使用Eclipse插件中的Chrome Developer调试
使用node-inspector调试:
使用 npm install -g node-inspector命令安装
使用node --debug-brk=5858 debug.js命令连接要调试的脚本
启动node-inspector
在浏览器里打开
建立http服务器:
//建立http服务器连接
var http=require('http');
http.createServer(function(req,res){
res.writeHead(200,{'Content-Type':'text/html'});
res.write('<h1>Node.js</h1>');
res.end('<p>Hello World</p>');
}
).listen(3000);
console.log("HTTP server is listening at port 3000.");