If your looking to write your node.js apps in coffeescript and run them on heroku you’ve most likely read this http://stackoverflow.com/questions/6356267/can-i-run-coffeescript-in-heroku question. It left me scratching my head wondering how they’ve made it sound so complicated. In reality its quite simple and I’ll show you.
We will use this sample CS/express app for testing:
Heroku’s new cedar runtime stack uses the existence of a package.json to detect your node.js app so we’ll go ahead and oblige it with a simple one like so:
Remember to add node_modules to your .gitignore file to keep your slug size down.
Now. Heroku expects a Procfile to instruct their servers how to run your application. Again this is pretty straight forward and absolutely not specific to coffee-script.
You’ll not that we’re telling heroku to boot a webworker by running a currently non-existant file ‘boot.js’. This is where the magic happens that turns your app from a node.js app to a node.js app written in coffeescript.
We can simply include coffee-script which augments require to allow it to load coffeescript files. Done and done.
Vows is a great node.js BDD framework. It’s very fast, running your tests asynchronously when possible, or sequentially when controlled execution order is required.
Its incredibly readable and easy to write in its own right but why would that stop us from writing our vows in CoffeeScript!
Taking the example from the vows website
We can write the same in CoffeeScript as below
Personally I think the CoffeeScript version is much more readable with less line noise. I don’t know about you but this how I’ll be writing my tests for node.js going forward!
For more information on vows be sure to check out there site. They go into how to write much more complicated tests. Apply what you leaned here to keep your tests caffeinated!
Lets look at some comparisons from http://coffeescript.org
And now the same thing in Coffeescript:
Now when I’ve been ranting about it to others in the industry they are skeptical that they should be writing in an intermediate language. All the while they comment about how much they love writing HAML and SASS. Now I know that there is a difference between Coffeescript and say Haml where one is being compiled into code and the other markup but the comparison stands.
* List compregensions in Coffeescript rock!
Gives us an alert box with [2,3,4,5,6]