For the past couple of months, I’ve been writing a task management application for myself using node.js, CoffeeScript, MongoDB, Mongoose, and several other tools. This is a post that covers a lot of that journey, including many of the different tools, packages, libraries, and articles that I ran across or found useful.
There are an overwhelming number of node modules and resources out there. I’ll cover some of the ones that I at least looked over at some point, and hopefully provide a bit less overwhelming guide to getting started. There are no code examples or step-by-step guides here - just a lot of links to pages where you can find them.
So, I wanted to try something new.
My top picks were Ruby on Rails, Python + Django (or similar), and node.js. I’ve played with Ruby on Rails a bit before and liked it. I like Python, and I’ve messed with Django a bit. I found node.js to be the most intriguing though.
I watched this video during the first week. It’s funny, and informative. I also started reading Node Web Development early on. It has some good bits & points including history & some interesting benchmark information, but the JS programming & style of the author was so bad that I stopped reading it. Although he proceeds to clean up the code somewhat in later chapters as he introduces additional tools, not prefacing the first code with a “Disclaimer: this code is shit. Don’t write code like this.” was basically criminal.
Books and reference material:
- The Node Beginner Book » A comprehensive Node.js tutorial
- Node.js Manual & Documentation
- Resources - joyent/node on GitHub
There are lots of options for database access. You could use a relational DB, or a NoSQL option. Some options:
- node-mongodb-native - used by Mongoose
- Mongoose ODM - Object to Document mapper with schemas & validation
I’m using Mongoose for my application.
Control flow / Async goodies
There’s a big list, again on the Node wiki.
I’m using async so far for several things. I definitely will need to revisit the list above though.
- Cluster - extensible multi-core server management for nodejs
- Writing Node.js Applications that Leverage Multiple Processors
- The Switch: Python to Node.js | Paul’s Journal
- Why Node.js rocks the middle-end | getiblog
- 6 Months with Node.js « hueniverse
- IBM doesn’t care about node.js peopleblog.nodejitsu.com - scaling node.js applications one callback at a time.
- If you’re using Node.js, you’re doing life wrong - Teh Journalz
- Alex Payne — Node and Scaling in the Small vs Scaling in the Large
I’ve started putting together a twitter list for node.js. It includes local people I know as well as others that I looked up or stumbled on. I’ve found NodeJS Community to be one of the better accounts to follow.
Some things just don’t need to be asynchronous. Common Node is something interesting to be aware of.
I explored a lot of logging options for node.
What I wanted:
- multiple log levels
- colorized terminal output
- multiple sinks (file, stdin, syslog, mongo DB)
- Apache-style log format
For colored output & log levels, I was digging coloured-log, but unfortunately it didn’t satisfy my other needs. The Swiss Army Knife of node logging seems to be winston. For now I’ve glued the two together, along with the logging built in to connect. I still need to revisit and clean this all up in to a solution that I’m happy with.
Error handling with node is messy for far. Some useful pages:
- error handling in asynchroneous node.js calls - Stack Overflow
- Node.js Best Practice Exception Handling - Stack Overflow
- node.js - Dealing with uncaught exceptions » Debuggable Ltd
Bootup & Restarting
Node applications have a tendency to crash easily if you don’t have solid exception handling in place. You may also want to have node start automatically at startup.
Right now, everything I’ve developed is just running on my development box. I’m planning to just put my application on my Amazon EC2 instance once I want it on the internet. As I’m comfortable with system admin and generally prefer the extra control, the relative benefit of a PaaS is smaller for me. This is something that I may reconsider later.
Some other misc. stuff:
- Node.js, MongoDB, CouchDB, etc. are implementations of this
Should You Learn CoffeeScript? | Nettuts+ looks like a good but long read.
Books and reference material include:
- CoffeeScript Cookbook.
- The Little Book on CoffeeScript
- Programming in CoffeeScript,
- Smooth CoffeeScript
Some good posts on the JS vs. CoffeeScript issue:
Some thoughts on style:
Some cool tools for CS include:
I have a good amount of relational DB experience - a lot with [MySQL], decent amounts with [PostgreSQL], [SQLite], and [MSSQL].
I could have used a SQL DB with node.js, but I figured this was a perfect opportunity to try a NoSQL DB.
One of the more popular options is MongoDB from 10gen, so I decided to hop on the bandwagon and give it a shot. Besides, MongoDB is Web Scale.
I started with the Ubuntu package for Mongo from
apt-get, but switched to the latest version using
10gen’s repo to get
rolling with the latest features.
can’t use SQL ;-) Backup and restore is also easy, and I set up some
make targets to automate this.
Instead of using mongo directly from my app, I opted to use Mongoose, which has worked great.
I attended the MongoDB LA conference in January as well, and picked up some useful information.
Haml is a lightweight markup language made popular by the Ruby community, that is converted to HTML.
On the CSS side, there is Sass, from the same guys.
Another option is LESS, which was influenced by Sass.
A lot of cool stuff seems to have come from Ruby on Rails. Rails 3.1 began bundling CS, jQuery, and Sass together.
If you’re using node though, you get jade (for HTML) and stylus (for CSS), which are even more compact than Haml, Sass, and LESS. They were created by the author of connect and express, and integrate nicely with them.
The most popular JS library is of course jQuery. I’ve used prototype, script.aculo.us and Dojo a fair amount in the past, but now it’s jQuery’s turn. For extra UI goodness, I’m starting with jQuery UI.
backbone.js, written by the author of CoffeeScript, and based on his popular
underscore.js library, is a popular choice. Other options include
The question Knockout.js vs Backbone.js (vs ?) on StackOverflow is good reading if considering these.
I picked backbone.js and have been using that so far for my new application. Although it looks more verbose than knockout.js, I’m interested in the routing & history features of Backbone. I liked the idea that it decouples your models from your views & the DOM a bit more as well. Backbone is compatible with jQuery as well as Zepto and Ender.
Useful backbone pages:
- recipe - What are your best practices for backbone.js projects? - Stack Overflow
- Building Epic Win With Backbone.js | CodeThinked
- Extensions, Plugins, Resources - GitHub
I’m planning on looking at Twitter Bootstrap more in the near future as well.
One of the most popular is Express.js which sits on top of
Get familiar with REST and CRUD if you’re going to use it - especially if you’re going to build an API. It comes with build in
DELETE, which means you can encapsulate the intention of your web requests
with existing HTTP verbs.
If you’re creating a JSON API, and sending JSON in the body of requests, enable the
and make sure you’re setting the
Content-Type header to
application/json for any requests from jQuery, etc.
To signal an error back to jQuery (and thus trigger the success or error callback), just send the appropriate
HTTP Status code.
If you’re using express (or connect), you can utilize
connect-assets (designed in the spirit of the Rails Asset Pipeline) to automatically compile your jade, stylus, and coffeescript
files, and include
<link> tags in your HTML via jade. Good stuff.
I did a fair amount of research in to testing options for my node.js application. Some of the options I came up with:
- Vows « Asynchronous BDD for Node
- Expresso - TDD Framework For Node
I started off with Jasmine, because I was using shellwords and it had some test code with it, so I was able to just look
at it from my
node_modules folder. But, I needed to do
and I didn’t like its approach so I
dropped it and moved to Vows.
I’d like to look at Mocha and Expresso a bit more (as everything else TJ Holowaychuk has made that I’ve used has been gold).
I haven’t gotten to looking at testing for the client-side yet.
Wrapping it Up
There are various frameworks that wrap up different chunks of the above:
- Express wiki - see the list of Frameworks built with Express
- Cham - Quick boilerplate for CoffeeScript app, backed by Express, Jade and Stylus
- Flatiron, A framework for Node.js - from the nodejitsu guys, which might be promising.
Here’s short list of people who’s names are worth knowing if you’re working in this space:
- Ryan Dahl - creator of node.js. Now works on research projects at Joyent.
- Isaac Schlueter - creator of NPM; now in charge of node.js
- Jeremy Ashkenas - creator of coffeescript, underscore, and backbone
- John Resig - creator of jQuery
- TJ Holowaychuk - creator of connect, express, jade, stylus, expresso, mocha, and more …
Here’s stuff I’ll probably be looking at soon:
- WebSocket and socket.io
- Mobile frameworks: PhoneGap, Titanium, Sencha Touch, etc.
- More testing
- More advanced Mongo DB: aggregation, map/reduce, replication, sharding, etc.
- More Mongoose: validation