Matt Kopala

Software Development, Technology, Travel

Getting Started With Node.js, Coffeescript, MongoDB, and More

| Comments

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.

Why Node.js?

I’ve been programming for a while, having programmed in over a dozen different languages. For web applications and websites, I first used Perl starting 14 years ago, and then switched to PHP and Javascript around 2004 as my language of choice for most things web. I did a fair amount of web programming for a few years, and then did a bunch of other stuff, and now I’m back to play with the web again. A lot has changed. Although I consider myself proficient in PHP, there’s no love affair there. The more I hack with other languages & frameworks (Ruby, Python, node.js) the less appealing PHP is. Although I’ve written a lot of test code in PHP as well, this still this nagging feeling that testing with PHPUnit sucks and always will suck.

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.

Node.js

Node.js is a server-side JavaScript application framework based on Google’s V8 JavaScript engine, originally written by Ryan Dahl of Joyent.

To get started, visit the node.js homepage. To install node packages, you’ll be using NPM.

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.

After not too long, I just sat down & read the node.js manual from front to back. I’m one of those that actually likes to RTFM.

Books and reference material:

Databases

There are lots of options for database access. You could use a relational DB, or a NoSQL option. Some options:

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.

Multiple Processors

Node is a single-threaded, event-driven, asynchronous I/O JavaScript server framework. If you need more than one core for node:

Blog articles

Twitter

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.

Synchronous Node?

Some things just don’t need to be asynchronous. Common Node is something interesting to be aware of.

Logging

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

Error handling with node is messy for far. Some useful pages:

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.

Some options here include nodejs-autorestart, fugue, node-init, launchtool, upstart, and monit.

Hosting

Some platform hosting options include Heroku, Nodejitsu, and Nodester. Joyent actually has a list on the node wiki.

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.

Javascript

It’s important to know your JavaScript before you get too far in with node.

Books:

If you want to kill a lot of time, you can go through the links on Douglas Crockford’s JavaScript page. He’s also the author of JSLint.

Some other misc. stuff:

  • DailyJS: A JavaScript Blog
  • CommonJS – project with the goal of specifying an ecosystem for JavaScript outside the browser
    • Node.js, MongoDB, CouchDB, etc. are implementations of this

Coffeescript

CoffeeScript is a little language that compiles in to JavaScript. It’s homepage is here. The syntax is very similar to python, and indentation is significant.

Should You Learn CoffeeScript? | Nettuts+ looks like a good but long read.

Books and reference material include:

I personally like CoffeeScript, and I plan to write most if not all of my JavaScript code in CS.

Some good posts on the JS vs. CoffeeScript issue:

Some thoughts on style:

Some cool tools for CS include:

  • js2coffee – converts JavaScript to CoffeeScript
  • Cake – simplified version of Make for CoffeeScript

MongoDB

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.

Mongo is super-easy to run and use, although I had to learn the JavaScript-based syntax for the command line, since you 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.

After a bit of searching for a GUI interface, I picked Rock Mongo, and it’s working out fine for now. I’m running Linux, so MongoVUE was out. There is a good list of Admin UIs on the mongo wiki.

I attended the MongoDB LA conference in January as well, and picked up some useful information.

Web Applications

If you’re writing something that is going to appear in the web browser, it’s going to end up having HTML, CSS, and JavaScript. But, this doesn’t mean you need to write in any of these languages. I’ve already covered CoffeeScript.

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.

For writing your client-side JavaScript, you can of course write in Coffeescript. But beyond that, especially if you’re writing a rich web application, you’ll probably want to utilize some frameworks and libraries.

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.

For Javascript MVC, backbone.js, written by the author of CoffeeScript, and based on his popular underscore.js library, is a popular choice. Other options include spine and Knockout.js.
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:

Other JS frameworks I found include SammyJS, JavaScriptMVC, AgilityJS, and SproutCore.

I’m planning on looking at Twitter Bootstrap more in the near future as well.

See also:

Server

One of the most popular is Express.js which sits on top of connect. 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 support for GET, POST, PUT, and 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 bodyParser middleware, 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 <script> and <link> tags in your HTML via jade. Good stuff.

Testing

I did a fair amount of research in to testing options for my node.js application. Some of the options I came up with:

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 asynchronous testing, 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.

See also:

Wrapping it Up

There are various frameworks that wrap up different chunks of the above:

People

Here’s short list of people who’s names are worth knowing if you’re working in this space:

What’s Next

Here’s stuff I’ll probably be looking at soon:

Conclusion

Obviously, there’s more than enough above to keep you busy for a while. It’s definitely a bit overwhelming at first, but it’s exciting how much is out there, and how fast node.js & JavaScript are growing. Have fun!

Comments