Skip to content
Dominic Barnes edited this page May 21, 2014 · 2 revisions

Client([base])

A helper for working with a CouchDB API. This includes initiating HTTP requests, handling authentication and sessions, as well as generating proper URLs. It is not typically for public use, but it is documented here if you desire to customize couchdb-api with methods for APIs not supported in core yet. (please submit PRs with those additions!)

The only param here is the base URL for the server. If nothing is specified, it will use http://localhost:5984/ as a default.

These are initialized for each [[Server]] instance. As other objects (like [[Database]], [[Document]], etc) are created, they will share the same Client instance. (as a result, you will need to create new instances for each user that you intend to have a session for, if you are using CouchDB's built-in authentication for your own application)

var couchdb = require("couchdb-api");

var srv1 = couchdb();
var db1 = srv1.db("test");
// srv1.client === db1.client

var srv2 = couchdb();
// srv1.client !== srv2.client

Client#basicAuth(user, pass)

This helper method will set up HTTP Basic Authentication for all subsequent requests made by this client. If you don't wish to use cookie/oauth/proxy authentication, and you're using a secure (HTTPS) transfer this is the easiest authentication scheme to use.

client.basicAuth("user", "pass");

Client#request(method, url)

Initiates a new HTTP request for this CouchDB server.

The method must be a superagent method name. (eg: "get", "del", etc) The url argument will be passed through Client#url().

This returns a raw superagent.Request object, so you can do things like append query arguments, attach a request body, etc.

// issue a GET request against the API root
var req = client.request("get", "/");

// issue a DELETE request against a database
var req = client.request("del", "/db");

Client#url([path])

Generates a URL for this CouchDB server.

If path is provided, it will append that to the base URL. (if it is an Array, it will be joined by /)

client.url();
// => http://localhost:5984/

client.url("db");
// => http://localhost:5984/db

client.url([ "db", "doc" ]);
// => http://localhost:5984/db/doc
Clone this wiki locally