node.js 基礎篇

日誌輸出方式 javascript

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

若是須要日誌文件追加 node test.js 2>>error.log 1>>info.logjava

若是是用 sublimeText-Nodejs 須要在 Nodejs.sublime-build 中修改如下節點(根據本身的操做系統)node

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

若是不設置,默認輸出到系統consolethis

日誌語法操作系統

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,統一保存在異常日誌中日誌

輸出某段代碼執行時間htm

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

httpblog

一個簡單的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);
  });          
});
相關文章
相關標籤/搜索