码迷,mamicode.com
首页 > Web开发 > 详细

node.js 基础篇

时间:2015-05-20 20:28:06      阅读:149      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!