Connecting to a CouchDB database using Node.js and Cradle
Although CouchDB provides a RESTful interface, you don't strictly need to make a database connection before using CouchDB; the Cradle module uses the notion of a connection to manage its internal state and there's still a connection object you need to create.
How to do it...
Here's how to include the Cradle module in your Node.js application and initialize it, getting a handle to a particular database:
var cradle = require('cradle'); var db = new(cradle.Connection)().database('documents');
How it works…
This code first includes the Cradle module, and then creates a new Cradle Connection
object, setting its database to the database documents
. This initializes Cradle with the default CouchDB host (localhost) and port (5984). If you need to override the host or port, you can do so by passing the host and port as the first and second arguments to the Connection
constructor, like this:
var connection = new(cradle.Connection)('http://example...