node package manager
We need your input. Help make JavaScript better: Take the 2017 JavaScript Ecosystem survey »


Koa.js Nunjucks

NPM Version Node Version License


$ npm install koajs-nunjucks

Middleware Setup

First, require koajs-nunjucks. It returns a function that will return a middleware generator.

var KoajsNunjucks = require('koajs-nunjucks');
var fMiddleware = KoajsNunjucks('', {});

The function takes two arguments. They are passed in the same order to the nunjucks.configure() function. Next, "use" the middleware function (before any other middleware that attempts to use it).

var app = require('koa')();
app.use( fMiddleware );


The render function is attached to the Koa context. It is availiable in other middleware functions directly as "this". The function renders the view and automatically sets it as the response body.

yield this.render('home.nj', { username: 'Bob' });

The render function takes two arguments as defined by nunjucks.render. It does NOT take a callback.

In additon to the render function, the enviorment is also attached to the context. Every request creates a fresh enviorment.