Below you will find pages that utilize the taxonomy term “Node.js”
Posts
'Mocha - the fun, simple, flexible JavaScript test framework'
Mocha 是 Node.js 上的單元測試框架。該單元測試框架能讓我們撰寫測試案例、運行單元測試、及產生測試報告等。
使用前先從 Registry 下載套件。
npm install mocha --save-dev 然後透過 Mocha 提供的 describe、it、before、after、beforeEach、afterEach 這幾個方法撰寫單元測試。
describe 用來設定描述測試的功能或情境,it 用來設定測試案例,before 用來設定測試情境下所有測試案例運行前要做的動作,after 用來設定測試情境下所有測試案例運行後要做的動作,beforeEach 用來設定每個測試案例運行前要做的動作,afterEach 用來設定每個測試案例運行後要做的動作。
像是下面這樣:
describe('Array', function() { describe('#indexOf()', function() { it('should return -1 when the value is not present', function() { ... }); }); }); 單元測試的斷言部份 Mocha 並未提供,需額外使用 Chai 之類的斷言套件搭配撰寫。
所以如果用 Mocha 搭配 Chai 撰寫,程式就會像下面這樣:
var assert = require('assert'); describe('Array', function() { describe('#indexOf()', function() { it('should return -1 when the value is not present', function() { assert.
read morePosts
Chai - A BDD / TDD assertion library for node
Chai 是 Node.js 的 BDD / TDD 斷言套件。
使用前先從 Registry 下載套件。
npm install chai --save-dev Chai 有三種撰寫風格,should、expect、assert,assert 是比較偏向傳統的斷言方式,expect 與 should 則是偏向 BDD style 的斷言方式。
無論哪個撰寫風格在撰寫前都需引用 Chai 套件。
const chai = require('chai'); should 撰寫風格使用上要先告知 Chai 套件使用 should 撰寫風格。
chai.should(); 接著用目標值應該是…、目標值應該等於…、目標值應該有…類似這樣的寫法撰寫斷言。
... target.should.be.a(type); target.should.eaequal(value); target.should.have.lengthOf(length); ... 程式寫起來會像下面這樣:
const chai = require('chai'); const foo = "bar"; const beverages = { tea: [ 'chai', 'matcha', 'oolong' ] }; chai.should(); foo.should.be.a('string'); foo.should.equal('bar'); foo.should.have.lengthOf(3); beverages.should.have.property('tea').with.lengthOf(3); 如果斷言錯誤,可以看到像下面這樣斷言錯誤的訊息。
read morePosts
Node.Js - Getting started
Node.Js 安裝完後,開個 Js 檔來撰寫簡單的 Hello World 程式。
console.log("Hello World"); 然後調用 node 命令並帶入程式位置。
node ${file} 即可運行程式,顯示程式運行後的結果。
再來看一下稍微複雜點的 Hello World。
var http = require("http"); var port = 3000; http.createServer(function(reqst, resp) { resp.writeHead(200, {'Content-Type': 'text/plain'}); resp.end('Hello World!'); }).listen(port); console.log('Load http://127.0.0.1:' + port + ' and watch the magic'); 會在特定 Port 號起一個 http 服務,該服務會回應 “Hello World!” 字樣。
將服務啟動。
用瀏覽器訪問服務的位置,即可看到服務回應我們預期的結果。
read morePosts
>-
cheerio 是一用來解析 HTML 的套件,該套件使用方式跟 jQuery 類似。
使用前需先用安裝套件。
npm install cherrio 安裝完後載入 cherrio 模組。
const cheerio = require('cheerio'); 然後用 load 方法將 HTML 載入,載入後就可以像一般使用 jQuery 般帶入 Selector 去選取元素操作。
const $ = cheerio.load(html) 像是下面這樣:
const cheerio = require('cheerio'); const $ = cheerio.load('<h2 class="title">Hello world</h2>') console.log($('h2').text()); 使用起來跟在一般網頁上用 jQuery 是差不多的。更多操作可參閱 cheeriojs/cheerio: Fast, flexible, and lean implementation of core jQuery designed specifically for the server. 這邊。
const cheerio = require('cheerio'); const $ = cheerio.load('<div class="title"><h2>Hello world</h2></div>') console.
read morePosts
Node.js - Transform stream
Transform stream 可以將輸入串流的資料讀入,將讀入的內容轉換,然後輸出到輸出串流。
像是內建的 Gzip transform stream 就能將輸入的資料做 Gzip 壓縮然後輸出。
const zlib = require('zlib'); const gzip = zlib.createGzip(); const fs = require('fs'); const input = fs.createReadStream('index.js'); const output = fs.createWriteStream('index.js.gz'); input.pipe(gzip).pipe(output); 如果要自行撰寫 Transform stream,我們可以撰寫個繼承自 Transform 的類別,然後在 _transform 內撰寫每次收到資料要做的處理,以及在 _flush 內撰寫清空緩衝區時要做的處理,在撰寫這兩個方法時,如果資料轉換完畢,可以用 this.push 將資料輸出。如果處理完成,則要記得調用 callback 方法。
'use strict'; const Transform = require('stream').Transform; class MyTransformStream extends Transform { _transform(data, encoding, callback) { ... //this.push(data); ... callback(); } _flush(callback) { ... //this.push(data); ... callback(); } } 像是筆者寫的這隻 AnalyzeStream,其功能為將 HTML 輸入,依照檢查的 Rule 分析,最後將分析的結果輸出。因為要分析的資料需要完整的 HTML,所以 _transform 這邊只有很簡單的將收到的資料存放起來,然後調用 callback 告知處理完成。在 _flush 這邊會將存放的資料套用 Rule 分析,將分析的結果用 this.
read morePosts
Mochawesome - A Gorgeous HTML/CSS Reporter for Mocha.js
Mochawesome 能讓 Mocha 支援產出 HTML 的測試報告。
使用前需安裝 Mochawesome 套件。
npm install --save-dev mochawesome 調用 Mocha 並使用 -reporter 參數指定使用 Mochawesome 產出測試報告。
mocha --reporter mochawesome 產出的測試報告會存放在 mochawesome-report 下的 mochawesome.html。
Link Mochawesome
read morePosts
bulk-require - require whole directory of trees in bulk
在撰寫 Node.js 時需要的模組我們需要載入才可以使用,bulk-require 套件能讓我們快速的載入目錄內的模組,不需要一個一個載入。
使用上需先安裝 bulk-require 套件。
npm install bulk-require 接著載入 bulk-require 模組。
const bulk = require('bulk-require'); 然後指定模組所在的目錄位置以及要載入的檔案 pattern 即可。
const sections = bulk(folderPath, ['*.js']); 像是下面這樣簡單的程式,我們載入了 folderPath 下所有副檔名為 js 的模組,並將載入的資訊顯示出來。
const bulk = require('bulk-require'); ... const sections = bulk(folderPath, ['*.js']); console.log(sections); 可看到載入的資訊會像下面這樣。
我們可以進一步利用這些載入的模組資訊,將載入的類別都建置實體,便於後續使用。
const bulk = require('bulk-require'); ... const sections = bulk(folderPath, ['*.js']); Object.keys(sections).forEach((element) => this.rules.push(new sections[element]()) ); Link substack/bulk-require: require whole directory of trees in bulk
read morePosts
n - Node version management
n 是一 Node version management,可以管理 Node.js 的版本。安裝時可先將 npm 的快取清掉。
sudo npm cache clean -f 然後用 npm 安裝 n 即可。
sudo npm install -g n 安裝好後就可以用 n 來進行 Node.js 版本的管理。
像是要使用最新版本的 Node.js。
n stable 要使用指定版本的 Node.js。
n <version> 要使用最新版本的 Node.js。
n latest 要切換已經安裝的 Node.js。
n 要移除指定版本的 Node.js。
n rm <version> 要移除當前位使用的 Node.js 版本。
n prune Link tj/n: Node version management Node.js各作業系統更新方式 - eddychang.me
read more