假设我有一组如下所示的路线:
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';
});
});
但这似乎不起作用(我也尝试过 plain routes.forEach
)。我究竟做错了什么?