xinshouke
xinshouke

Reputation: 537

What's the recommend code for koa-ejs using koa2?

I planed use ejs in koa2, my codes were like as blelow:

render(app, {
  root: path.join(__dirname, 'views-ejs'),
  layout: 'layout',
  viewExt: 'ejs',
  cache: false,
  debug: true
});

app.use(function *() {
  yield this.render('index',{
    title: 'koa2 title',
    viewClass: 'landing',
    targetAuthLevel:'',
    authorizationLevel:'6',
    ngController: 'landingController'
  });
});

But, I get the below warning, would you tell me what's codes are recommended? please.

koa deprecated Support for generators will been removed in v3. See the documentation for examples of how to convert old middleware https://github.com/koajs/koa/tree/v2.x#old-signature-middleware-v1x

Upvotes: 1

Views: 2126

Answers (2)

You can avoid this error with updating koa-ejs to "next" version:

$ npm rm -S koa-ejs
$ npm i -S koa-ejs@next
  • rm alias for remove command
  • i alias for install command
  • -S alias for --save key

Upvotes: 0

Lee Benson
Lee Benson

Reputation: 11599

Per the README:

Workaround for Koa 2

npm install co --save

Then...

import co from 'co';
import render from 'koa-ejs';

render(app, options);
app.context.render = co.wrap(app.context.render);

app.use(async (ctx, next) => {
    await ctx.render(view, locals);
});

Upvotes: 1

Related Questions