如何使用REST / JSON / GET / POST从我的节点服务器访问我的couchdb?

时间:2011-08-23 13:05:43

标签: 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.log('Server running at http://127.0.0.1:80/');

我想向nodejs服务器发出RESTful http和POST请求。然后,nodejs服务器应该能够向Couchdb发出GET / POST请求,Couchdb使用JSON对象进行响应。

我该怎么做?

4 个答案:

答案 0 :(得分:3)

首先,我是nano的作者,并将在此回复中使用它。

这里有一些简单的说明来开始使用node.js和CouchDb。

mkdir test && cd test
npm install nano
npm install express

如果你安装了couchdb,那很好。如果不这样做,您将需要安装它在iriscouch.com

在线设置实例

现在创建一个名为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设置了usernamepassword,则需要将其包含在网址中。在以下行中,我将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");

您现在可以手动创建,甚至可以通过编程方式创建。如果您对如何实现这一目标感到好奇,可以阅读我之前写的这篇文章Nano - Minimalistic CouchDB for node.js

有关详细信息,请参阅expressjsnano。希望这有帮助!

答案 1 :(得分:2)

您可以使用Cradle之类的node.js模块来处理CouchDB。

以下是可用的Node.JS模块列表:https://github.com/joyent/node/wiki/modules

答案 2 :(得分:2)

只需发出HTTP请求。我建议request

以下是from my code

的示例
request({
    "uri": this._base_url + "/" + user._id,
    "json": user,
    "method": "PUT"
}, this._error(cb));

这是另一个例子from my code

// save document
"save": function _save(post, cb) {
    // doc changed so empty it from cache
    delete this._cache[post.id];

    // PUT document in couch
    request({
        "uri": this._base_url + "/" + post._id,
        "json": post,
        "method": "PUT"
    }, this._error(function _savePost(err, res, body) {
        if (body) {
            body.id = post.id;
            body.title = post.title;
        }
        cb(err, res, body);
    }));
}

答案 3 :(得分:2)

我有一个模块(node-couchdb-api)我是为了这个目的写的。它没有ORM或其他类似的功能,它只是CouchDB提供的HTTP API的简单包装器。它甚至遵循Node.JS为异步回调建立的约定,使您的代码更加一致。 :)