20

I am trying to connect to my CouchDB database on Cloudant using Node.js.

This worked on the shell:

    curl https://weng:password@weng.cloudant.com/my_app/_all_docs

But this node.js code didn't work:

    var couchdb = http.createClient(443, 'weng:password@weng.cloudant.com', true);
    var request = couchdb.request('GET', '/my_app/_all_docs', {
        'Host': 'weng.cloudant.com'
    });
    request.end();
    request.on('response', function (response) {
        response.on('data', function (data) {
            util.print(data);
        });
    });

It gave me this data back:

    {"error":"unauthorized","reason":"_reader access is required for this request"}

How do I do to list all my databases with Node.js?

700 Software
  • 85,281
  • 83
  • 234
  • 341
ajsie
  • 77,632
  • 106
  • 276
  • 381

4 Answers4

18

The built-in Node.js http client is pretty low level, it doesn't support HTTP Basic auth out of the box. The second argument to http.createClient is just a hostname. It doesn't expect credentials in there.

You have two options:

1. Construct the HTTP Basic Authorization header yourself

var Base64 = require('Base64');
var couchdb = http.createClient(443, 'weng.cloudant.com', true);
var request = couchdb.request('GET', '/my_app/_all_docs', {
    'Host': 'weng.cloudant.com',
    'Authorization': 'Basic ' + Base64.encode('weng:password')
});
request.end();
request.on('response', function (response) {
    response.on('data', function (data) {
        util.print(data);
    });
});

You will need a Base64 lib such as one for node written in C, or a pure-JS one (e.g. the one that CouchDB Futon uses).

2. Use a more high-level Node.js HTTP client

A more featureful HTTP client, like Restler, will make it much easier to do the request above, including credentials:

var restler = require('restler');
restler.get('https://weng.cloudant.com:443/my_app/_all_docs', {
    username: 'weng',
    password: 'password'
}).on('complete', function (data) {
    util.print(data);
});
Steadicat
  • 309
  • 2
  • 7
  • Also, if you are calling this programmatically, you should create an API key. And give that key permissions to the database. Databases -> Docs (Pull-Down) -> Permissions. Add your key and give it 'Reader/Writer/etc...' access. https://my_company.cloudant.com/dashboard.html#/database/my-database/permissions – Bernie Perez Sep 09 '14 at 03:33
7

There are lots of CouchDB modules for Node.js.

Baggz
  • 17,207
  • 4
  • 37
  • 25
  • 1
    node-couchdb RE-update: now maintained & active has commits showing on may 12, 2012 – pulkitsinghal Jun 08 '12 at 18:47
  • [Cloudant's FAQ](https://cloudant.com/for-developers/faq/development/) _What library should I use for [insert_language_here]?_ also suggests [Sag](http://www.saggingcouch.com/), that is specifically tested against Cloudant – xverges Apr 24 '14 at 16:50
5

Just wanted to add

  • nano - minimalistic couchdb driver for node.js

to the list. It is written by Nuno Job, CCO of nodejitsu, and actively maintained.

zemirco
  • 16,171
  • 8
  • 62
  • 96
-1

This answer is looking a bit dated. Here is an updated answer that I verified using the following Cloudant Supported NPM Node Client library that works. https://www.npmjs.com/package/cloudant#getting-started

And to answer his question on how to list his databases use the following code.

//Specify your Cloudant Database Connection URL. For Bluemix format is: https://username:password@xxxxxxxxx-bluemix.cloudant.com

dbCredentials_url = "https://username:password@xxxxxxxxx-bluemix.cloudant.com"; // Set this to your own account 

// Initialize the library with my account. 
// Load the Cloudant library. 
cloudant = require('cloudant')(dbCredentials_url);

// List the Cloudant databases
cloudant.db.list(function(err, allDbs) {
console.log('All my databases: %s', allDbs.join(', ')) });
Carlos Ferreira
  • 1,980
  • 2
  • 14
  • 18