My Node.js Express + CoffeeScript setup

Last week I blogged about some of the reasons I like to use Node.js for Web development.

Today I thought I’d share some details  on how I start new projects with Express.js and CoffeeScript. Writing this post also helped me review and improve my workflow a bit which is always nice.

Basic setup

I start by creating a new express project and converting it to use CoffeeScript. To do so, we’ll need to install express, coffee-script and js2coffee globally:

npm install -g express coffee-script
npm install -g git://github.com/elisee/js2coffee.git

The official js2coffee module being currently broken, I forked it and applied 2 pull requests (#194 and #183) in my own fork to make it work with recent versions of CoffeeScript and on Windows. When those are merged upstream, I’ll be able to revert back to install js2coffee with:

npm install -g js2coffee

Once those modules are installed globally, I can go head and create my project (I like to use -c stylus to write Stylus stylesheets and -s to enable sessions support):

express $PROJECT_NAME -c stylus -s

cd $PROJECT_NAME
js2coffee app.js > app.coffee
rm app.js

Then we need to edit package.json to change:

"start": "node app.js"

to read:

"start": "coffee app.coffee"

At this point, the default Express app should run. We can install the dependencies and launch it through the node package manager like so:

npm install .
npm start

Serving JS transpiled from CoffeeScript

Whenever I want to add more dependencies to my Node.js app, I used to edit my package.json file manually and then run npm install . to install all new dependencies, but that’s a waste of time. It’s much easier to let npm handle it all:

npm install $SOME_PACKAGE --save

This will install the module locally but also add it as a dependency in package.json.

In order to be able to not only write CoffeeScript for server-side scripting but for all scripts, even those served to the client, I usually install coffee-script and express-coffee like so:

npm install coffee-script --save
npm install git://github.com/elisee/express-coffee.git --save

I’m using my own fork on the express-coffee module (which allows compiling CoffeeScript down to JS before serving them) because the original has gone unmaintained and doesn’t work with more recent versions of CoffeeScript. Just like for js2coffee, npm is flexible enough to let me directly pull my version of the module from git and it will properly reference it in package.json for consistent deployment.

Actually serving up the scripts can be done by adding the middleware like so:

env = app.get 'env'
app.use require('express-coffee')(
 path: path.join( __dirname, 'public' )
 live: env != 'production'
 uglify: env == 'production'
 debug: env != 'production'
 )

Finding useful modules

There are countless awesome (and less awesome) Node.js modules, most of them under a permissive MIT license.

When I’m looking for something, I do a search on https://npmjs.org/, look at various alternatives, using their download stats as a reference, dig into README & GitHub repositories to see if the module suits my needs, is built soundly enough and is well-maintained. When I need to, I can always fork, fix bugs myself and update the package.json dependency to use my own fork while I wait for the maintainer to look at my pull request and publish an updated official version of the package. I’m never stuck waiting for someone else!

A few examples of modules I find useful:

Getting ready for actual development

At this point, I generally clean up the default stylesheets & routes (and commit it all to Mercurial / Git) before getting started on my actual app.

I enjoy Mercurial more (combined with TortoiseHg & Bitbucket.org) so for personal / private projects I’ll use it but if I were to create a module for sharing on npm, I would most likely host it on GitHub, seeing as that’s where the node.js community hangs out.

Hope you’ll find it all useful. For more posts & updates about my projects, you can follow me on Twitter @elisee and/or @craftstudiodev