added: ctx.state as the recommended namespace for passing information through middleware and routes.

This commit is contained in:
Matthew Mueller 2014-11-18 00:54:14 -08:00
parent 193eadbdb5
commit 6847fe68bd
3 changed files with 30 additions and 0 deletions

View file

@ -50,6 +50,14 @@ app.use(function *(){
A koa `Response` object.
### ctx.state
The recommended namespace for passing information through middleware and to your frontend views.
```js
this.state.user = yield User.find(id);
```
### ctx.app
Application instance reference.

View file

@ -146,6 +146,7 @@ app.createContext = function(req, res){
context.originalUrl = request.originalUrl = req.url;
context.cookies = new Cookies(req, res, this.keys);
context.accept = request.accept = accepts(req);
context.state = {};
return context;
};

21
test/context/state.js Normal file
View file

@ -0,0 +1,21 @@
var request = require('supertest');
var assert = require('assert');
var koa = require('../..');
describe('ctx.state', function() {
it('should provide a ctx.state namespace', function(done) {
var app = koa();
app.use(function *() {
assert.deepEqual(this.state, {});
});
var server = app.listen();
request(server)
.get('/')
.expect(404)
.end(done);
})
})