使用 Node.js 连接到 Cloudant CouchDB?
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
内置的 Node.js http 客户端水平相当低,它不支持开箱即用的 HTTP 基本身份验证。
http.createClient
的第二个参数只是一个主机名。它并不期望那里有凭证。您有两个选择:
1。自己构建 HTTP 基本授权标头
您将需要一个 Base64 库,例如 用 C 编写的节点,或者纯 JS 的(例如 CouchDB Futon 使用的那个)。
2.使用更高级的 Node.js HTTP 客户端
功能更强大的 HTTP 客户端,例如 Restler ,将使执行上述请求变得更加容易,包括凭据:
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
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:
Node.js 有很多 CouchDB 模块。
There are lots of CouchDB modules for Node.js.
只是想将
添加到列表中。它由 nodejitsu 的 CCO Nuno Job 编写,并积极维护。
Just wanted to add
to the list. It is written by Nuno Job, CCO of nodejitsu, and actively maintained.
这个答案看起来有点过时了。这是我使用以下有效的 Cloudant 支持的 NPM 节点客户端库验证的更新答案。
https://www.npmjs.com/package/cloudant#getting-started
要回答他关于如何列出他的数据库的问题,请使用以下代码。
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.