xinshouke xinshouke - 5 months ago 48
Node.js Question

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

Answer

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);
});
Comments