basic setup for express + handlebars

This is a basic guide on getting started with node.js using express. You can use tools to automate the creation of express apps but I decided to do it manually as you get more idea of what is going on so if/when you encounter problems you have more idea than you would if you blindly used a tool.

create a new working directory

mkdir newexpress
cd newexpress

install express

npm install express

node_modules/express/bin/express
npm install

install handlebars for express

npm install express3-handlebars --save

rm views/*
cp -R node_modules/express3-handlebars/examples/basic/views/* views/

enable handlebars templating

edit app.js

add

var exphbs = require('express3-handlebars');

replace

app.set('view engine', 'jade');

with

app.engine('handlebars', exphbs({defaultLayout: 'main'}));
app.set('view engine', 'handlebars');

remove or comment out default routes

var routes = require('./routes');
var user = require('./routes/user');

app.get('/', routes.index);
app.get('/users', user.list);

add new routes

app.get('/', function (req, res) {
    res.render('home');
});

You should now be able to start the app with ” node app “.
The example views are basic but you can easily add in ‘partials’ to extend layouts.

This can also be extended to include other modules like sass etc.
I personally have extended it to use grunt + sass + livereload just to make is easier for designing sites.

UPDATE: 22nd March 2014

I have now switched from express3-handlebars to express-hbs (the module ghost uses).