如何使用REST/JSON/GET/POST从节点服务器访问couchdb?
我正在尝试从node.js服务器访问我的couchdb 我已经学习了nodejs教程,并设置了这个简单的nodejs服务器:如何使用REST/JSON/GET/POST从节点服务器访问couchdb?,json,http,rest,node.js,couchdb,Json,Http,Rest,Node.js,Couchdb,我正在尝试从node.js服务器访问我的couchdb 我已经学习了nodejs教程,并设置了这个简单的nodejs服务器: var http = require('http'); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n'); }).listen(80, "127.0.0.1"); console.l
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(80, "127.0.0.1");
console.log('Server running at http://127.0.0.1:80/');
我想向nodejs服务器发出restfulhttp和POST请求。然后,nodejs服务器应该能够向Couchdb发出GET/POST请求,Couchdb使用JSON对象进行响应
我该怎么做呢?您可以使用node.js模块,例如与CouchDB一起工作
下面是可用Node.JS模块的列表:只需发出HTTP请求即可。我推荐 这里有一个例子 这是另一个例子
我有一个模块()就是为了这个目的而写的。它没有ORM或其他类似的特性,它只是CouchDB提供的HTTP API的简单包装器。它甚至遵循Node.JS为异步回调建立的约定,使您的代码更加全面一致。:) 首先,我是
nano
的作者,并将在本次回复中使用它
下面是一些简单的说明,以开始使用node.js和CouchDb
mkdir test && cd test
npm install nano
npm install express
如果您安装了couchdb,那就太好了。如果您不这样做,您将需要安装它或安装一个实例在线
现在创建一个名为index.js
的新文件。在内部放置以下代码:
var express = require('express')
, nano = require('nano')('http://localhost:5984')
, app = module.exports = express.createServer()
, db_name = "my_couch"
, db = nano.use(db_name);
app.get("/", function(request,response) {
nano.db.create(db_name, function (error, body, headers) {
if(error) { return response.send(error.message, error['status-code']); }
db.insert({foo: true}, "foo", function (error2, body2, headers2) {
if(error2) { return response.send(error2.message, error2['status-code']); }
response.send("Insert ok!", 200);
});
});
});
app.listen(3333);
console.log("server is running. check expressjs.org for more cool tricks");
如果您为CouchDB设置了用户名
和密码
,则需要将其包含在url中。在下一行中,我在url中添加了admin:admin@
,以举例说明
, nano = require('nano')('http://admin:admin@localhost:5984')
此脚本的问题在于,它会在每次执行请求时尝试创建数据库。这将在您第一次创建它时失败。理想情况下,您希望从脚本中删除create database,使其永远运行:
var express = require('express')
, db = require('nano')('http://localhost:5984/my_couch')
, app = module.exports = express.createServer()
;
app.get("/", function(request,response) {
db.get("foo", function (error, body, headers) {
if(error) { return response.send(error.message, error['status-code']); }
response.send(body, 200);
});
});
});
app.listen(3333);
console.log("server is running. check expressjs.org for more cool tricks");
您现在可以手动创建,甚至可以编程方式创建。如果你对如何实现这一目标感到好奇,你可以阅读我不久前写的这篇文章
有关更多信息,请参阅和。希望这有帮助 大家都知道摇篮有问题,真的吗?给我们一些关于这个的链接,我根本无法得到
DELETE
请求来使用摇篮,它们可以使用request
(有时)。一般来说,DELETE
对coach的请求被认为是节点有缺陷的。不知道为什么。谢谢你,你这个光荣的混蛋!这对我有用。FSM知道为什么,但我一直在苦苦思索,想弄明白这件事。不知道为什么所有其他节点扩展都不起作用,但这一个起作用了!非常感谢。
, nano = require('nano')('http://admin:admin@localhost:5984')
var express = require('express')
, db = require('nano')('http://localhost:5984/my_couch')
, app = module.exports = express.createServer()
;
app.get("/", function(request,response) {
db.get("foo", function (error, body, headers) {
if(error) { return response.send(error.message, error['status-code']); }
response.send(body, 200);
});
});
});
app.listen(3333);
console.log("server is running. check expressjs.org for more cool tricks");