问题
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
回答1:
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);
});
回答2:
You can avoid this error with updating koa-ejs to "next" version:
$ npm rm -S koa-ejs
$ npm i -S koa-ejs@next
rmalias forremovecommandialias forinstallcommand-Salias for--savekey
来源:https://stackoverflow.com/questions/36976834/whats-the-recommend-code-for-koa-ejs-using-koa2