A quick tip for the Couch (CouchDB that is)

I have been playing around with CouchDB lately as a new tecnology. It is a NoSQL database that stores everything as a ‘document’, rather than as a row like a traditional relationship based database, such as MySQL. What is also cool about CouchDB is that it uses a REST API. That is, every request and operation is done through HTTP, so it works with essentially every language. NoSQL is new to me, but I am liking it so far!

This rhino wishes he had a comfortable couch to lay on.

I am actually using my CouchDB instance as both my database and webserver. Since CouchDB has a REST API and everything in CouchDB is a document, so there is no reason why you cannot simply ues the browser to send an HTTP request and return HTML pages directly from the database! Pretty neat to eliminate Apache, PHP, and MySQL from the web stack and instead use just CouchDB.

Anyways, I have been chugging along merrily, serving static HTML pages or show functions from my database, until I started to use the list functions that CouchDB offers.

Specifically, I was having trouble getting it to return an HTML header, rather than just plaintext.

To solve this, you must format your list function as:

function(head, req)
{
    provides('html', function() {
    ... The list function ...
    send(data);
});
}

This tells the list function to send HTTP headers along with the data, rather than just the data itself.

This took me a while to figure it out, so hopefully it helps you!

About samkerr

I'm an eclectic person. I like to dabble in a multitude of things. I'm sure you'll find my blog reflects that.
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *