使用 Node.js 连接到 Cloudant CouchDB?

发布于 2024-09-30 07:08:28 字数 1021 浏览 6 评论 0原文

我正在尝试使用 Node.js 连接到 Cloudant 上的 CouchDB 数据库。

这在 shell 上有效:

    curl https://weng:[email protected]/my_app/_all_docs

但是这个 node.js 代码不起作用:

    var couchdb = http.createClient(443, 'weng:[email protected]', 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);
        });
    });

它给了我这些数据:

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

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

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

This worked on the shell:

    curl https://weng:[email protected]/my_app/_all_docs

But this node.js code didn't work:

    var couchdb = http.createClient(443, 'weng:[email protected]', 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦忆晨望 2024-10-07 07:08:28

内置的 Node.js http 客户端水平相当低,它不支持开箱即用的 HTTP 基本身份验证。 http.createClient 的第二个参数只是一个主机名。它并不期望那里有凭证。

您有两个选择:

1。自己构建 HTTP 基本授权标头

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);
    });
});

您将需要一个 Base64 库,例如 用 C 编写的节点,或者纯 JS 的(例如 CouchDB Futon 使用的那个)。

2.使用更高级的 Node.js HTTP 客户端

功能更强大的 HTTP 客户端,例如 Restler ,将使执行上述请求变得更加容易,包括凭据:

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);
});

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);
});
烟柳画桥 2024-10-07 07:08:28

Node.js 有很多 CouchDB 模块。

There are lots of CouchDB modules for Node.js.

忆梦 2024-10-07 07:08:28

只是想将

  • nano - 用于 Node.js 的简约 couchdb 驱动程序

添加到列表中。它由 nodejitsu 的 CCO Nuno Job 编写,并积极维护。

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.

离线来电— 2024-10-07 07:08:28

这个答案看起来有点过时了。这是我使用以下有效的 Cloudant 支持的 NPM 节点客户端库验证的更新答案。
https://www.npmjs.com/package/cloudant#getting-started

要回答他关于如何列出他的数据库的问题,请使用以下代码。

//Specify your Cloudant Database Connection URL. For Bluemix format is: https://username:[email protected]

dbCredentials_url = "https://username:[email protected]"; // 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(', ')) });

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:[email protected]

dbCredentials_url = "https://username:[email protected]"; // 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(', ')) });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文