dynamic路线在科阿?

比方说,我有一个看起来像这样的路线数组:

var routes = [ { route: '/', handler: function* () { this.body = yield render('home', template.home) } }, { route: '/about', handler: function* () { this.body = yield render('about', template.about) } } ]; 

什么是最好的方式来app.use它们? 我尝试过这样做(用koa-route作为我的中间件):

 Promise.each(routes, function(r) { app.use(route.get(r.route, r.handler)); }).then(function() { app.use(function *NotFound(next) { this.status = 404; this.body = 'not found'; }); }); 

但它似乎并没有工作(我也尝试了一个普通的routes.forEach )。 我究竟做错了什么?

经过一些修补,我已经设法通过这样做得到上面的代码工作:

 var routes = { '/': function* () { this.body = yield render('home', template.home); }, '/about': function* () { this.body = yield render('about', template.about); } }; app.use(function* respond() { if (routes[this.request.url]) yield routes[this.request.url].call(this); }); 

我会尽可能地接受这个答案,但是如果有人提出更好的解决scheme,我会乐于接受他们的。