io.js的六大新特性

io.js是nodejs的友好版的分支("friendly fork」)。它支持npm中全部的一樣模塊,且使用了v8最新版本的截取(v8是被node.js使用js解釋器),且修復了不少的bug,下面咱們將討論這些新特性:node

 

在運行程序以前的預加載模塊redis

新的node/iojs二進制有一個新的CLI選項用於在運行程序以前預加載模型。npm

-r--要求模塊在啓動的時候預加載app

這對於 預加載項目日誌和調試你電腦中的模塊頗有用測試

例如:ui

// preload.js
var myLogger = require('./myredislogger')('remotehost', 8678)

console.log = function () {
  console.log.apply(console.log, arguments)
  myLogger.log.apply(myLogger.log, arguments)
}

console.error = function () {
  console.error.apply(console.error, arguments)
  myLogger.error.apply(myLogger.error, arguments)
}

> node -r preload.js server.js

 

代碼設置高效的UID/GIDthis

在大對數的可移植操做系統接口(posix)系統上面,高效的uid/gid(euid/egid)是程序建立文件的全部者,且程序使用其進行access檢查(access checks)。如今,這些檢查和設置能夠在io,js中經過代碼實現,例以下例:spa

process.geteuid()
process.seteuid(id)
process.getegid()
process.setegid(id)

 

享受簡單化的流創造操作系統

當你使用io.js穿件一個新地簡單化的建立(simplified creation API),你不須要在留上面設置難懂的underscore方法。調試

var transform = new stream.Transform({
  transform: function(chunk, encoding, next) {
    // sets this._transform under the hood
  },
  flush: function(done) {
    // sets this._flush under the hood
  }
});

 

使用dns.lookup()的'all'參數獲取全部的DNS結果

如今在結局域名問題的時候,你能夠獲取全部的結果,而不是隻是得到第一個或默認的結果。

dns.lookup('localhost', {all:true}, function (err, results) {
  console.log(results)
  // [ { address: '127.0.0.1', family: 4 },
  //   { address: '::1', family: 6 },
  //   { address: 'fe80::1', family: 6 } ]
})

 

使用Buffer.indexOf() 

這和string上面的indexOf()相似,除了對Buffers不能使用。

在書寫二進制轉換器的時候buffer頗有用。

 

assert.deepStrictEqual() 適用於更好的測試

The assert module commonly used in testing has a deepEqual() function which quite useful but use '==' and not '==='. assert.deepStrictEqual has the same deep recursive functionality but uses '==='.

 

原文連接:https://blog.xervo.io/fun-with-iojs-6-new-features

相關文章
相關標籤/搜索