Up-to-date, executable code for two great tutorials on rolling a blog with express, Node.js and a noSQL database: https://github.com/victorkane/couchblogtut
The first, Blog rolling with mongoDB, express and Node.js, takes you “through the steps required to get a fully-functional (albeit feature-light) persistent blogging system running on top of node. It was first published on February 18, 2010 by Ciaran Jessup and last updated May 14, 2010.
The second, Blog rolling with CouchDB, Express and Node.js, was published on February 7, 2011 by Ian Wootten, and was commented on just this last April.
This pedagogical repo brings it up to date (as of June 22, 2011), makes a MongoDB branch basing the persistence on MongoDB; and a CouchDB branch (ultimately merged into Master) after the more recent article.
Pre-requisites
- Have Node.js, CouchDB and NPM (the Node.js package manager) installed on your system. See my article Setting up Cloud App Server with CouchDB, Node.js and Express on Ubuntu 10.04 LTS Part I for step-by-step instructions on how to do this.
- Install MongoDB if you wish to run the mongodb branch of the repo. See these wiki notes from my Node.js literary workshop project (just getting started) on how I did this on my VPS.
Instructions
- Check out from the GitHub repo and install the dependencies (the npm install command works thanks to the contents of package.json):
$ git clone git@github.com:victorkane/couchblogtut.git
$ cd couchblogtut/
$ npm install
jade@0.12.3 ./node_modules/jade
mime@1.2.2 ./node_modules/express/node_modules/mime
qs@0.1.0 ./node_modules/express/node_modules/qs
connect@1.5.1 ./node_modules/express/node_modules/connect
express@2.3.11 ./node_modules/express
vargs@0.1.0 ./node_modules/cradle/node_modules/vargs
eyes@0.1.6 ./node_modules/cradle/node_modules/vows/node_modules/eyes
vows@0.5.8 ./node_modules/cradle/node_modules/vows
cradle@0.5.5 ./node_modules/cradle
For CouchDB version, create database and view prior to running
Using Futon (although you can do it on the command-line of course) I first created the database articles and (although it was not necessary) a single article document with fields title and body. I then created the view as part of a permanent design document as follows:
{
"_id": "_design/articles",
"_rev": "1-49e3c6709a33d23b8efd3cacceefd6fa",
"language": "javascript",
"views": {
"all": {
"map": "function(doc) {n emit(doc._id, doc);n}"
}
}
}
To do this, I went to Overview and clicked on the articles database from the link on the right-hand side. I selected Temporary view… from the View drop-down list, and edited it as follows:
function(doc) {
emit(doc._id, doc);
}
I clicked Run and it correctly listed the document I had created earlier. I then clicked on Save As… and entered “articles” in the Design Document field (reading “_design/articles” and “all” in the View Name field, and clicked Save.
Run and point your browser at http://localhost:3000
$ node app.js
Express server listening on port 3000
Packages used on master (couchdb) branch
$ cat package.json
{
"name": "couchblogtut"
, "version": "0.0.1"
, "private": true
, "dependencies": {
"express": "2.3.11"
, "jade": ">= 0.0.1"
, "cradle": "0.5.5"
}
}
Packages used on mongodb branch
$ git checkout mongodb
Switched to branch 'mongodb'
learner@li209-15:~/couchblog$ cat package.json
{
"name": "couchblogtut"
, "version": "0.0.1"
, "private": true
, "dependencies": {
"express": "2.3.11"
, "jade": ">= 0.0.1"
, "mongodb": "0.9.6"
}
I think with the mongodb branch I ran it without having to create anything in the database itself first.
Running the code without persistence
If you want to run the code as it was in the commit just prior to adding in the support for one of the databases, with seudo-persistence in memory, check out that commit as a branch and run it as follows:
$ git checkout nodb
$ node app.js
Or, something you can do with the first ten characters or so of any commit hash, creating a local branch in order to follow along step by step:
$ git checkout -b nodb 485ad191f1c
$ node app.js
So, you can follow along in the original articles and see the up-to-date versions of the code, and run them. Enjoy! Let me know if you need any help here in the comments.