Send to Kindle

Monday, April 12, 2010

Quick Intro to node.couchapp.js

‹prev | My Chain | next›

Thanks to some pointers from Mikeal himself, I have some insight on how to work with node.couchapp.js (a node.js reimplementation of couchapp). First up, there seems to be a common starting point:
var couchapp = require('couchapp');

var ddoc = {_id:'_design/app', shows:{}, updates:{}, views:{}, lists:{}};
exports.app = ddoc;
I am not sure if that is enough to create something, but let's give it a try:
cstrom@whitefall:~/tmp/test_node_couchapp_js$ node ~/repos/node.couchapp.js/lib/bin.js \
-s -d foo -c http://localhost:5984/seed
Syncing app finished.
Nice! That's a lot further than I got after an hour of messing about last night. It always helps to have an example or two. Taking a look at the document inside CouchDB's futon, I find:



Interesting, so the simple definition, then export of the ddoc variable is sufficient to create the attributes of the design document.

I wonder if I need to define those attributes at all. Probably not, so I delete them and add a simple show document to make sure that I can do so:
var couchapp = require('couchapp');

var ddoc = {_id:'_design/app', shows:{}};
exports.app = ddoc;

ddoc.shows.foo = function (doc, req) {
return "<h1>Foo!</h1><p>Bar.</p>";
};
I load that design document again:
cstrom@whitefall:~/tmp/test_node_couchapp_js$ node ~/repos/node.couchapp.js/lib/bin.js \
-s -d foo -c http://localhost:5984/seed
Syncing app finished.
(good to know that it can be re-run)

Checking the show document in futon, I find that the extra attributes have indeed been removed without harm:



And, checking that the show document can indeed show documents, I visit the show document proper. I am uploading to the seed database, accessing the _design/app design document, the foo _show document. This translates into a URL of: http://localhost:5984/seed/_design/app/_show/foo. Visiting the URL, I do indeed see the simple show document I hoped to see:



Cool! Well, that was a lot easier than I thought it was going to be after flailing last night. Thanks Mikeal!

Day #71

2 comments:

  1. I'm very glad to find your post, however, I find it hard to follow... lost you on the way...

    Just to understand what you're doing, the file on the start-point file is what you save as "~/repos/node.couchapp.js/lib/bin.js" or is some npm module that runs your start-point file ?
    Is "~/repos/node.couchapp.js/" your work dir, or a utility called from your work dir?

    And can you tell a little about the switches "-s -d foo -c http://localhost:5984/seed"?

    ReplyDelete
  2. Sorry for confusion. Most of that was written in response to the previous night's work: http://japhr.blogspot.com/2010/04/spike-nodecouchappjs.html (which also explains the command line switches).

    This was 2 years ago, so I am unsure if any of this still applies, but... I never touched the bin.js file -- that came from a cloned github repo. I was doing this in a previous empty working directory. The code went into a file named `foo.js`. Hope that is of some help!

    ReplyDelete