首页 > Web开发 > 详细

node.js 基础篇

时间:2015-05-20 20:26:37      阅读:301      评论:0      收藏:0      [点我收藏+]

日志输出方式 

node test.js 2>error.log 1>info.log 

如果需要日志文件追加 node test.js 2>>error.log 1>>info.log

如果是用 sublimeText-Nodejs 需要在 Nodejs.sublime-build 中修改以下节点(根据自己的操作系统)

"cmd": ["taskkill /F /IM node.exe & node $file 2>>error.log 1>>info.log", ""] 

如果不设置,默认输出到系统console

日志语法

console.log(‘Server running at http://127.0.0.1:8888/‘);
console.info(‘text: %s !‘, message);
console.error(‘this is a error‘);
console.warn(‘this is a warn‘);

node.js中日志中无法区分warn或者error,统一保存在异常日志中

输出某段代码执行时间

console.time("hi");
console.log("it works!");
console.timeEnd("hi");  

http

一个简单的http服务

var http = require(‘http‘);
http.createServer(function (request, response) {   
  response.writeHead(200, {‘Content-Type‘: ‘text/html‘});
  response.end(‘Hello World\n‘);
}).listen(8888);
console.log(‘Server running at http://127.0.0.1:8888/‘);

一个简单的http客户端

http.get({
  hostname: ‘localhost‘,
  port: 8888,
  path: ‘/‘,
  agent: false  // create a new agent just for this one request
}, function (res) {
  var data = ‘‘;
  res.on(‘data‘, function (chunk){            
  	data += chunk.toString();
  });
  res.on(‘end‘,function (){
      console.log("data is:"+data);
  });          
});
http.get(‘http://localhost:8888‘,function (res) {
  var data = ‘‘;
  res.on(‘data‘, function (chunk){            
  	data += chunk.toString();
  });
  res.on(‘end‘,function (){
      console.log("data is:"+data);
  });          
});

  

  

 

node.js 基础篇

原文:http://www.cnblogs.com/zhanghaoh/p/4517938.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!