Node.js and CouchDB feel like they were made for each other right from the very first time I used them. With the cradle node package the integration becomes even easier.

Whilst both Node.js and CouchDB are open source with packages for most operating systems it maybe easier for you to start out using a hosted solution such as CloudNo.de (has CouchDB now) or Nodester for example. As far as the CouchDB portion goes there is only one place to go and that is IrisCouch.

With the exception of IrisCouch they are all in private beta so there might be a small wait before you get access.

I wrote the following example code before CloudNo.de supported CouchDB so it references IrisCouch, but the setup is similar.

So at this point I am going to assume that you have successfully installed Node.js and CouchDB or are using hosted services. All the projects have good installation documentation for most platforms and there are many blog posts rehashing this step out there.

Firstly you will need to install the cradle package using:

npm install cradle

It should be noted here that if you are using a hosted service the command will be something like:

cloudnode app npm install cradle

from the applications root directory (the one with the .git folder in it). Both the CloudNo.de documentation and Nodester documentation cover this process on their respective websites.

CouchDB is documented on the Apache foundation’s web pages and the cradle middleware has its source and documentation on GitHub.

var http = require('http');
http.createServer(function (req, http_res) {
  http_res.writeHead(200, {'Content-Type': 'text/plain'});
  var response = '';

  var cradle = require('cradle');
  var connection = new(cradle.Connection)('https://subdomain.iriscouch.com', 443, {
      auth: { username: 'username', password: 'password' }
  });

  var db = connection.database('database_name');
  db.save('document_key', {
      name: 'A Funny Name'
  }, function (err, res) {
      if (err) {
          // Handle error
          response += ' SAVE ERROR: Could not save record!!\n';
      } else {
          // Handle success
          response += ' SUCESSFUL SAVE\n';
      }
      db.get('document_key', function (err, doc) {
          response += ' DOCUMENT: ' + doc + '\n';
          http_res.end(response);
      });
  });
}).listen(8071);

I think that the code is fairly easy to follow if you have written JavaScript code utilising callbacks before, but I will step through it for clarity.

A new HTTP server is setup first so that we can access the Node.js programme through the a web browser to trigger the database changes. Incidentally this would be accessed at http://localhost:80871 or http://subdomain.cloudno.de.

Cradle is then setup and the connection parameters are set with the CouchDB database being selected on line 11.

Next a new document is saved into the DB (replace document_key with your document name to customise) with the contents of { name: “A funny name” }. Once this is saved a callback function is called that will attempt to retrieve the record.

Depending upon how successful the whole process is you should see something like

SUCESSFUL SAVE
DOCUMENT: { name: "A funny name" }

in your browser.

As you can see it is really easy to change what is saved and how it is retrieved. This was just a very simple introduction, but there are many neat tricks documented at each projects homepage.