Blog Project(2)Express Backend API - istanbul - mocha - bunyan

BlogProject(2)ExpressBackendAPI-istanbul-mocha-bunyan

There2MongoUITools:mongohubandrebomongo

SetUpProjectBasic

gulp

istanbul

http://gotwarlost.github.io/istanbul/

http://www.ruanyifeng.com/blog/2015/06/istanbul.html

http://www.jianshu.com/p/e297d2eceb05

http://blog.csdn.net/csr0312/article/details/47044259

Installistanbulonmylocal

>sudonpminstall-gistanbul

>catsimple.js

vara=1;

varb=1;

if((a+b)>2){

console.log('morethantwo');

}

>istanbulcoversimple.js

=============================================================================

Writingcoverageobject[/Users/carl/work/nodejs/nodejstest/coverage/coverage.json]

Writingcoveragereportsat[/Users/carl/work/nodejs/nodejstest/coverage]

=============================================================================

===============================Coveragesummary===============================

Statements:75%(3/4)

Branches:50%(1/2)

Functions:100%(0/0)

Lines:75%(3/4)

================================================================================

linecoverage-ifeverylineisexecuted

functioncoverage-ifallfunctionsarecalled

branchcoverage-ifall‘if’statementsgetexecuted

statementcoverage-ifallstatementsgetexecuted

Thereisahtmlreportaswell

>opencoverage/lcov-report/index.html

Setstandardtobe90%

>istanbulcheck-coverage--statement90

ERROR:Coverageforstatements(75%)doesnotmeetglobalthreshold(90%)

Mocha

Weusuallyuseistanbulwithmocha

Installmochainglobal

>sudonpminstall-gmocha

installchaionlocal

>npminstallchai

Simplesqrt.js

>catsqrt.js

varMy={

sqrt:function(x){

if(x<0){

thrownewError("cannotbenegative");

}

returnMath.exp(Math.log(x)/2);

}

};

module.exports=My;

Simpletestscodes

>cattest/test.sqrt.js

varchai=require('chai');

varexpect=chai.expect;

varMy=require('../sqrt.js');

describe("sqrt",function(){

it("4sqrtshouldbe2",function(){

expect(My.sqrt(4)).to.equal(2);

});

it("throwexceptionifxisnegative",function(){

expect(function(){My.sqrt(-1);}).to.throw("cannotbenegative");

});

});

Worktogether

>istanbulcover_mocha

sqrt

✓4sqrtshouldbe2

✓throwexceptionifxisnegative

2passing(8ms)

=============================================================================

Writingcoverageobject[/Users/carl/work/nodejs/nodejstest2/coverage/coverage.json]

Writingcoveragereportsat[/Users/carl/work/nodejs/nodejstest2/coverage]

=============================================================================

===============================Coveragesummary===============================

Statements:100%(5/5)

Branches:100%(2/2)

Functions:100%(1/1)

Lines:100%(5/5)

================================================================================

LoggingSystem-bunyan

https://github.com/trentm/node-bunyan

http://www.ctolib.com/node-bunyan.html#installation

Installation

>npminstallbunyan--save

>sudonpminstall-gbunyan

SimpleusageoftheLogger

>cathi.js

varbunyan=require('bunyan');

varlog=bunyan.createLogger({name:'myapp'});

log.info("hi");

log.warn({lang:'fr'},'hello');

Showthelogging

>nodehi.js

{"name":"myapp","hostname":"ip-10-10-21-215.ec2.internal","pid":28178,"level":30,"msg":"hi","time":"2017-06-08T15:20:22.400Z","v":0}

{"name":"myapp","hostname":"ip-10-10-21-215.ec2.internal","pid":28178,"level":40,"lang":"fr","msg":"hello","time":"2017-06-08T15:20:22.402Z","v":0}

Logginginniceformat

>nodehi.js|bunyan

[2017-06-08T15:22:22.449Z]INFO:myapp/28340onip-10-10-21-215.ec2.internal:hi

[2017-06-08T15:22:22.451Z]WARN:myapp/28340onip-10-10-21-215.ec2.internal:hello(lang=fr)

>nodehi.js|bunyan-lwarn

[2017-06-08T15:22:43.041Z]WARN:myapp/28359onip-10-10-21-215.ec2.internal:hello(lang=fr)

>nodehi.js|bunyan-c'this.lang=="fr"'

[2017-06-08T15:23:24.429Z]WARN:myapp/28468onip-10-10-21-215.ec2.internal:hello(lang=fr)

CommonsTool

https://lodash.com/

https://lodash.com/docs/4.17.4

http://wwsun.github.io/posts/lodash-top-10-functions.html

Markdown-it

https://github.com/markdown-it/markdown-it

https://markdown-it.github.io/

Passport

http://blog.fens.me/nodejs-express-passport/

Hereistheversionofemptyprojects

https://github.com/luohuazju/sillycat-blog-backend-express/tree/version-1

References:

https://github.com/jackhutu/jackblog-api-express

相关推荐