I have a Koa2 application, which renders templates on different routes. I'd like to introduce a middleware which will modify rendered templates in some way and I need it to be the last in a chain of other middlewares. Are there any way to enforce some middleware to be applied last before firing response using Koa2 and without modification of the already defined routes?
I have tried the code below:
// modification middleware
app.use(async function (ctx, next) {
await next();
ctx.body = ctx.body.toUpperCase();
})
// template rendering
app.use(async function (ctx, next) {
const users = [{ }, { name: 'Sue' }, { name: 'Tom' }];
await ctx.render('content', {
users
});
});
app.listen(7001);
It works as expected, but if any other middleware will be introduced before the modification
one, it will be not the last in the chain.
Is it possible to achieve described behavior?