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 ...

Get JavaScript JSON Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.