Dust.js is a nice template engine for Node.js, but its development has been idle for almost a year.
Now I’m trying to test it on Express with Node.js 0.6.x, and the easiest way turns out to be using consolidate.js.
Consolidate.js only support express.js 3.0.xbranch, which is not onnpm` yet, so we need to install the CLI from master branch on github
$ npm install -g https://github.com/visionmedia/express/tarball/master
Then create a default Express app named test:
$ express -s -e test && cd test
Now we need to install express module also from masterbranch on github:
$ npm install https://github.com/visionmedia/express/tarball/master
Then do regular npm install to install dependencies.
Consolidate.js author has not pushed dust.js support to npm, so we need to install from master branch.
$ npm install https://github.com/visionmedia/consolidate.js/tarball/master
Then install dust.js
$ npm install dust
There are a few changes you need to do before being able to run the app. See the below app.js for changes (old ones are commented out).
var express = require('express')
, routes = require('./routes')
, http = require('http')
, fs = require('fs')
, path = require('path')
, cons = require('consolidate');
var app = express();
// assign dust engine to .dust files
app.engine('dust', cons.dust);
app.configure(function(){
app.set('view engine', 'dust');
app.set('views', __dirname + '/views');
app.use(express.favicon());
app.use(express.logger('dev'));
//app.use(express.static(__dirname + '/public'));
app.use(express.static(__dirname + '/public', {redirect: false}));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser('your secret here'));
app.use(express.session());
app.use(app.router);
});
app.configure('development', function(){
app.use(express.errorHandler());
});
//app.get('/', routes.index);
app.get('/', function(req, res){
res.render('index', {
title: 'Testing out dust.js server-side rendering'
});
});
http.createServer(app).listen(3000);
console.log("Express server listening on port 3000");
You also need to modify Dust to add support for Node 0.6.x by modifying node_modules\dust\lib\server.js as following:
- Script = process.binding('evals').Script;
+ Script = require("vm");
- require.paths.unshift(path.join(__dirname, '..'));
Then you can now run node app.js.