Teaching myself node.js: Part 1

I’ve been meaning for a while to learn node.js. I read a couple of books and plenty of blog posts but in the end I find, as usual, I can only learn by doing. I have most of the fundamentals to get started, I just need to have a project. The typical tutorials I’ve found focus on creating a blog; I’m thinking of something more along the lines of a Quora question/answer site. I’ll be trying to write this as I go, which will hopefully prod me into actually doing it this time. (I have a tendency to bounce around different tenses and I expect this will be worse than usual – stuff will likely be written in past/present/future at various points).

Installing node is pretty easy, I just downloaded the source (I used 0.4.7), configured & compiled it. For MongoDB I use the 10gen CentOS/Fedora repository. With node installed, I installed npm to facilitate adding other packages. I started mongod with

/etc/init.d/mongodb start

With everything installed and mongod running I thought about the schema of my object. One of the coolest things about MongoDB is the fact that it’s “schemaless” and you can basically put anything into the DB, so you just need to think about what your objects look like and you can basically shove them into Mongo in that exact format. I’ve never been a fan of ORM stuff for SQL and the Mongo document-store type of DB seems like it eliminates the need for that almost entirely, which is great. So here’s what I was thinking would be the central object in this q-a site:

        "_id" : ObjectId("4defd2713fce2e9bc35d09f0"),
        "author" : "Bob",
        "date" : ISODate("2011-06-08T19:49:48.809Z"),
        "body" : "Is it hot in here?",
        "votes" : 0,
        "answers" : [
                        "author" : "evan",
                        "date" : ISODate("2011-06-08T19:48:54.388Z"),
                        "body" : "I think so.",
                        "votes" : 0
                        "author" : "Jim",
                        "date" : ISODate("2011-06-08T19:51:04.714Z"),
                        "body" : "Could be.",
                        "votes" : 0
                        "author" : "Aaron",
                        "date" : ISODate("2011-06-08T19:51:33.313Z"),
                        "body" : "No",
                        "votes" : 0
        "tags" : [

So this is a “question” and it has an author, a date, an array of tags, a title, a vote count, a “body” (maybe a longer version of the question), and an array of “answers”. Answers are embedded objects, each with an author, date, body, and votes. This seems like a decent place to start.

The way the site would work is someone posts a question and other people answer it. The answers would just be attached to the post in the “answers” array. Other users can vote on answers and the answer with the most votes would be the “winner”. Not exactly groundbreaking, but seems like it should be pretty easy to implement.

I used npm to install the following packages to aid in development:

$ npm install express jade mongoskin underscore

Express is a framework that handles request routing and jade is a templating framework. I used this quick walkthrough to setup a basic express/jade app that works and demonstrates most of what I need to know to get this started. Another good tutorial is http://dailyjs.com/2010/11/08/node-tutorial-2/.

After some mucking around I figured out how to use Express properly and it made things much easier. I used mongoskin as the MongoDB library and jade for the template and managed to rig up something that responds to a “list” request at http://server:3000/questions by listing all the questions in the “questions” collection. My code as of this point is available on github here. The views/list.jade has the template for the question list, and the code that calls the template is in app.js.

When viewing in a web browser, it currently looks like this (YMMV based on what’s in your mongodb, of course):


That’s enough for today. So far I’m enjoying node.js now that I think I can finally wrap my head around the callback stuff. Hopefully I can keep up the momentum.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s