add app.toJSON()

This commit is contained in:
TJ Holowaychuk 2014-03-11 11:01:33 -07:00
parent f0e353d6a0
commit 895cf4040c
3 changed files with 34 additions and 2 deletions

View file

@ -13,6 +13,7 @@ var Cookies = require('cookies');
var accepts = require('accepts');
var assert = require('assert');
var http = require('http');
var only = require('only');
var co = require('co');
/**
@ -68,6 +69,22 @@ app.listen = function(){
return server.listen.apply(server, arguments);
};
/**
* Return JSON representation.
*
* @return {Object}
* @api public
*/
app.toJSON = function(){
return only(this, [
'outputErrors',
'subdomainOffset',
'poweredBy',
'env'
]);
};
/**
* Use the given middleware `fn`.
*
@ -197,4 +214,4 @@ function *respond(next){
this.length = Buffer.byteLength(body);
if (head) return res.end();
res.end(body);
}
}

View file

@ -30,7 +30,8 @@
"fresh": "~0.2.1",
"koa-compose": "~2.2.0",
"cookies": "~0.4.0",
"delegates": "0.0.3"
"delegates": "0.0.3",
"only": "0.0.2"
},
"devDependencies": {
"should": "~3.1.0",

View file

@ -47,6 +47,20 @@ describe('app', function(){
})
})
describe('app.toJSON()', function(){
it('should work', function(){
var app = koa();
var obj = app.toJSON();
obj.should.eql({
outputErrors: false,
subdomainOffset: 2,
poweredBy: true,
env: 'test'
});
})
})
describe('app.use(fn)', function(){
it('should compose middleware', function(done){
var app = koa();