io.js是nodejs的友好版的分支("friendly fork”)。它支持npm中所有的同樣模塊,且使用了v8最新版本的截取(v8是被node.js使用js解釋器),且修復了很多的bug,下面我們將討論這些新特性:
在運行程序之前的預加載模塊
新的node/iojs二進制有一個新的CLI選項用於在運行程序之前預加載模型。
-r--要求模塊在啟動的時候預加載
這對於 預加載項目日志和調試你電腦中的模塊很有用
例如:
// 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/GID
在大對數的可移植操作系統接口(posix)系統上面,高效的uid/gid(euid/egid)是程序創建文件的所有者,且程序使用其進行access檢查(access checks)。現在,這些檢查和設置可以在io,js中通過代碼實現,例如下例:
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