Using Twitter Bootstrap with Node.js, Express and Jade

I’ve decided to write this post as a note to myself. I’m still learning Node.js and digging into Express/Jade, but I’ve read many people using the nice Twitter Bootstrap and I was wondering if there was a way to integrate all these technologies. The short answer is: yes, we can!

Note: once again, I’m not a Node.js expert and surely there are other ways to achieve this task (for example there is a Node.js module called twitter-bootstrap, but I haven’t tried it). This tutorial is based on another tutorial I found, but it was not very updated and it had a more complicated way to install Bootstrap, so I decided to write a new one basing it on the original http://www.rs.au.com/31/how-to-install-bootstrap-v2-0-2-in-expressjs-v3-0-0

Preparing the environment

I will assume that you’re running any Linux distribution (in my case I’m using Ubuntu 12.10, but feel free to use your own distribution). Be sure to have installed a recent version of nodejs and npm packages (I’m using Node.js 0.8.20 and npm 1.2.11).

Create a project folder and install the required dependencies

Create the basic project structure with Express

You should already have installed all the needed dependencies, even without executing npm install, anyway executing it won’t hurt.

Download and install Bootstrap

Download Twitter Boostrap from the official website http://twitter.github.com/bootstrap/assets/bootstrap.zip and unzip it under the nodebootstrap/public folder.

Bootstrap integration with Jade template system

At this point you need to edit the views/layout.jade file and include the references to Bootsrap

Test the Bootstrap integration

At this point we will modify views/index.jade that is the default template used to render the index

extends layout

Now go back to the terminal and execute the app:

Open your favourite browse and visit http://localhost:3000 to see your first Bootstrap + Node.js application app and running.