queckezz/koa-views

support koa v2

tejasmanohar opened this issue · 7 comments

support koa v2

Any update on this?

For now, you can use a work around like this:

const co = require('co');
const Koa = require('koa');
const views = require('koa-views');
const convert = require('koa-convert');

app.use(convert(views('views', {
  map: {
    html: 'nunjucks'
  }
})));
app.use(async (ctx, next) => {
  ctx.render = co.wrap(ctx.render);
  await next();
});

and this work for me

app.use(convert(views('views', {
  root: __dirname + '/views',
  default: 'jade'
})));

app.use(co.wrap(function *(ctx, next){
  ctx.render = co.wrap(ctx.render);
  yield next();
}));

@chentsulin is it that your example right? I caught error when running it.

app.use(async (ctx, next) => {
              ^

SyntaxError: Unexpected token (
    at exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:374:25)
    at Object.Module._extensions..js (module.js:405:10)
    at Module.load (module.js:344:32)
    at Function.Module._load (module.js:301:12)
    at Function.Module.runMain (module.js:430:10)
    at startup (node.js:141:18)
    at node.js:1003:3

@yreenchan At this moment, you should transpile your async await code with babel until v8 support it out of box.

Sadly I'm not really up to date with this. I saw @tj's comment over at koa koajs/koa#533 and he basically states that we should wait to upgrade middleware until async-await lands in v8.

Mentioned somewhere else, but I think we should wait to upgrade middleware until async/await is native, otherwise we'll just be going through them all over again to upgrade. Plus doing it now is confusing in the sense that it makes it seem like people should be migrating, even though 2.0 is not a thing yet (IMO shouldn't be until async/await).

@chentsulin solution is the way to go atm (koa-convert + babel 6).

Added a koa v2 example to the readme based on @chentsulin solution. Thanks!