当前位置: 首页 > 工具软件 > bunyan > 使用案例 >

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

凌照
2023-12-01
Blog Project(2)Express Backend API - istanbul - mocha - bunyan

There 2 Mongo UI Tools: mongohub and rebomongo

Set Up Project Basic
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

Install istanbul on my local
>sudo npm install -g istanbul

>cat simple.js
var a = 1;
var b = 1;
if ((a + b) > 2) {
console.log('more than two');
}

>istanbul cover simple.js
=============================================================================
Writing coverage object [/Users/carl/work/nodejs/nodejstest/coverage/coverage.json]
Writing coverage reports at [/Users/carl/work/nodejs/nodejstest/coverage]
=============================================================================

=============================== Coverage summary ===============================
Statements : 75% ( 3/4 )
Branches : 50% ( 1/2 )
Functions : 100% ( 0/0 )
Lines : 75% ( 3/4 )

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

line coverage - if every line is executed
function coverage - if all functions are called
branch coverage - if all ‘if’ statements get executed
statement coverage - if all statements get executed

There is a html report as well
>open coverage/lcov-report/index.html

Set standard to be 90%
>istanbul check-coverage --statement 90
ERROR: Coverage for statements (75%) does not meet global threshold (90%)

Mocha
We usually use istanbul with mocha
Install mocha in global
>sudo npm install -g mocha
install chai on local
>npm install chai

Simple sqrt.js
> cat sqrt.js
var My = {
sqrt: function(x) {
if (x < 0){
throw new Error("can not be negative");
}
return Math.exp(Math.log(x)/2);
}
};

module.exports = My;

Simple tests codes
>cat test/test.sqrt.js
var chai = require('chai');
var expect = chai.expect;
var My = require('../sqrt.js');

describe("sqrt", function() {

it("4 sqrt should be 2", function() {
expect(My.sqrt(4)).to.equal(2);
});

it("throw exception if x is negative", function() {
expect(function(){ My.sqrt(-1); }).to.throw("can not be negative");
});

});

Work together
>istanbul cover _mocha


sqrt

✓ 4 sqrt should be 2


✓ throw exception if x is negative


2 passing (8ms)


=============================================================================
Writing coverage object [/Users/carl/work/nodejs/nodejstest2/coverage/coverage.json]
Writing coverage reports at [/Users/carl/work/nodejs/nodejstest2/coverage]
=============================================================================

=============================== Coverage summary ===============================
Statements : 100% ( 5/5 )
Branches : 100% ( 2/2 )
Functions : 100% ( 1/1 )
Lines : 100% ( 5/5 )

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

Logging System - bunyan
https://github.com/trentm/node-bunyan
http://www.ctolib.com/node-bunyan.html#installation
Installation
>npm install bunyan --save
>sudo npm install -g bunyan

Simple usage of the Logger
>cat hi.js
var bunyan = require('bunyan');
var log = bunyan.createLogger({name: 'myapp'});

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

Show the logging
>node hi.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}

Logging in nice format
>node hi.js | bunyan
[2017-06-08T15:22:22.449Z] INFO: myapp/28340 on ip-10-10-21-215.ec2.internal: hi
[2017-06-08T15:22:22.451Z] WARN: myapp/28340 on ip-10-10-21-215.ec2.internal: hello (lang=fr)

>node hi.js | bunyan -l warn
[2017-06-08T15:22:43.041Z] WARN: myapp/28359 on ip-10-10-21-215.ec2.internal: hello (lang=fr)

>node hi.js | bunyan -c 'this.lang == "fr"'
[2017-06-08T15:23:24.429Z] WARN: myapp/28468 on ip-10-10-21-215.ec2.internal: hello (lang=fr)

Commons Tool
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/

Here is the version of empty projects
https://github.com/luohuazju/sillycat-blog-backend-express/tree/version-1

References:
http://sillycat.iteye.com/blog/2378442
https://github.com/jackhutu/jackblog-api-express
 类似资料: