我有一个简单的koa.js设置与koa-route和koa-ejs。
var koa = require('koa');
var route = require('koa-route');
var add_ejs = require('koa-ejs');
var app = koa();
add_ejs(app, {…});
app.use(function *(next){
console.log( 'to do layout tweak for all requests' );
yield next;
});
app.use(route.get('/', function *(name) {
console.log( 'root action' );
yield this.render('index', {name: 'Hello' });
}));
在这两种方法之间传递值的最佳方法是什么?
答案 0 :(得分:10)
答案 1 :(得分:1)
您可以使用Koa Context:
app.use(function *(next) {
this.foo = 'Foo';
yield next;
});
app.use(route.get('/', function *(next) { // 'next' is probably what you want, not 'name'
yield this.render('index', { name: this.foo });
yield next; // pass to the next middleware
}));