xinshouke xinshouke - 9 months ago 98
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',
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


Per the README:

Workaround for Koa 2

npm install co --save


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