起手式
4.0 以後,需要另外安裝 express-generator。

npm install -g express-generator  
npm install --save express


其實這邊小矛盾,express-generator 要裝到 global,而 express 則要裝到 local

產生專案,css 用 stylus ,template engine 用 ejs

express -c stylus -e your_project 


-c 可以選 less/sass/stylus ,不加就是 plain
-e 是 ejs,不加,預設是 jade ,還可以選其他 template engine,需要另外設定。

TDD/BDD

內建有 assert,但不是很口語化。


var assert = require('assert');
var expected, current;
before(function() {
expected = ['a', 'b', 'c'];
});
describe('String#split', function() {
beforeEach(function() {
current = 'a,b,c'.split(',');
});
it('should return an array', function() {
assert(Array.isArray(current));
});
it('should return the same array', function() {
assert.equal(expected.length, current.length, 'array have equal length');
for(var i=0; i<expected.length; i++) {
assert.equal(expected[i], current[i], i+'element is equal');
}
});
});

可以加裝 chai/expect ,會更口語化。


var expect = require('expect.js');
var expected, current;
before(function() {
expected = ['a', 'b', 'c'];
});
describe('String#split', function() {
beforeEach(function() {
current = 'a,b,c'.split(',');
});
it('should return an array', function() {
expect(Array.isArray(current)).to.be.true;
});
it('should return the same array', function() {
expect(expected.length).equal(current.length);
for(var i=0; i<expected.length; i++) {
expect(expected[i]).equal(current[i]);
}
});
});


簡單說就是 up to you 。 主要搭配的工具是 mocha,這個要裝在 global:npm install -g mocha

第三章最後有介紹如何測試網頁,是使用 superagent,在網路上找到一篇 simple test first express setup & tutorial

這篇也是用 mocha,主要是用 supertest 來做測試,另外比較特別的一點是用 supervisor 來啟動 server,supervisor 會幫你監視程式的變動,然後自動重新啟動。

packages.json

在這檔案裡也可以放一些預設的指令,例如 npm start 就是啟動 nodejs application server,所以可以在此加入 test,這樣就可以 npm test 進行測試。
arrow
arrow

    elleryq 發表在 痞客邦 留言(0) 人氣()