koajs / koa-hbs

Handlebars templates for Koa.js

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

koa-hbs has order dependency when used with koa-router

desenmeng opened this issue · comments

Hi, jwilm

I use koa-hbs(0.4.4) in koa(0.5.5)

app.use(middlewares.bodyparser());
app.use(middlewares.jsonp());
app.use(middlewares.router(app));
app.use(middlewares.compress());
app.use(hbs.middleware({
    viewPath: __dirname + '/views',
    partialsPath: __dirname + '/views/partials',
    layoutsPath: __dirname + '/views/layouts'
}));

// init routes
app.get('/', function*() {
     this.render('index', {title: 'test'});
});

//run app
app.listen(3000);
console.log('listening on port 3000');

when I vist localhost:3000,I found the error

TypeError: Object #<Object> has no method 'render'

but app.use is well

app.use(hbs.middleware({
    viewPath: __dirname + '/views',
    partialsPath: __dirname + '/views/partials',
    layoutsPath: __dirname + '/views/layouts'
}));

//// init routes
//app.get('/', function*() {
//     this.render('index', {title: 'test'});
//});

app.use(function*(){
   yield this.render('index', {title: 'test'});
});

I need use koa-hbs before koa-router

I also found the problem,thanks.
but why?

This is a dependency chain issue with koa-router and koa-compose. It's been around a while and is well-known. It isn't ideal, but the easy workaround is to app.use koa-hbs as soon in the stack as is possible. If someone knows how to hack around the limitations, we'd happily accept a PR.