5

I am trying to render an object into html using node,express and mongodb. when I tried the below code

var express=require('express'),
app=express(),
engines = require('consolidate'),
nunjucks  = require('nunjucks');

app.set('view engine', 'html');
app.engine('html', engines.nunjucks);
app.set('views', __dirname + '/views');

app.get('/',function(req,res){
res.render('index',{'name':'Hello'});
})

app.use(function(req,res){
res.sendStatus(404);
})

var server = app.listen(3000,function(){
var port=server.address().port;
console.log('Express server listening on port', port);
})

I am getting callback function error like below

D:\Wiki>node app.js
D:\Wiki\node_modules\express\lib\application.js:174
if ('function' != typeof fn) throw new Error('callback function required');
                       ^

Error: callback function required
at Function.app.engine (D:\Wiki\node_modules\express\lib\application.js:174:
38)
at Object.<anonymous> (D:\Wiki\app.js:7:5)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Function.Module.runMain (module.js:441:10)
at startup (node.js:139:18)
at node.js:968:3

can someone help me how to render html using nunjucks? Thanks in advance

maverick
  • 73
  • 1
  • 8

4 Answers4

2

As you can see in log - error on line 7 app.engine('html', engines.nunjucks);. So problem is here.

Add new file e.g. templateEngine.js

var nunjucks  = require('nunjucks');

module.exports = function (app) {
    // store environment env
    var env = nunjucks.configure(['views/', 'views2/'], {
        autoescape: true,
        express   : app
    });

    // This is filter example. You can use it by {{varname | myFilter}} in template
    env.addFilter('myFilter', function(text) {
        return text.toUpperCase();
    });
}

In main js call require('templateEngine')(app);

Aikon Mogwai
  • 4,954
  • 2
  • 18
  • 31
  • it says "cannot find module 'templateEngine' – maverick Jul 31 '16 at 09:50
  • sry, I had to include "./" before templateEngine. Then I am not getting that error. But what does "callback function required" error mean? – maverick Jul 31 '16 at 09:54
  • 1
    I think it's means that call `app.engine('html', engines.nunjucks)` is not correct because engines.nunjucks is not a function as required. – Aikon Mogwai Jul 31 '16 at 10:04
2

Hello fellow mongo university student! I faced the same issue as well when started on this week 1 lecture. The cause was I installed an incorrect version of engines dependency when writing the code from scratch.

Solution is to just clear your project's local node_modules and have just app.js in the root and views folder with the template.

like this :

hello_world
 --app.js
 --views
 ----index.html

now install the dependencies one by one

npm install express
npm install consolidate
npm install nunjucks

Finally

node app.js  // to start the app.

Also you dont have to have nunjucks as separate dependency in line 4, considate, takes care of this for you. You should be able to see the app come up fine.

pRmdk
  • 151
  • 1
  • 10
0

Had the same problem, solved by:

1) npm install consolidate nunjucks --save

2) define consolidate in app.js/server.js/index.js or whatever you name your main app file:

var consolidate = require('consolidate');

3) Middleware should be:

app.engine('html', consolidate.nunjucks);
app.set('views', './views');
HelloWoRlD
  • 129
  • 4
  • 12
0

From nunjucks docs:

Using express? Simply pass your express app into configure: var app = express();

nunjucks.configure('views', {
    autoescape: true,
    express: app
});

So you can do (assuming you have a views folder in the same directory containing this file):

{code...}
var app = express();
var nunjucks = require('nunjucks');

nunjucks.configure(__dirname + '/views', {...

app.set('view engine', 'html');
{...}

Note the use of nodejs __dirname to resolve the path.

1565986223
  • 6,420
  • 2
  • 20
  • 33