簡體   English   中英

如何使用REST / JSON / GET / POST從我的節點服務器訪問我的couchdb?

[英]How can I access my couchdb from my node server using REST/JSON/GET/POST?

我正在嘗試從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對象進行響應。

我怎么能這樣做?

首先,我是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@添加到url以舉例說明

, 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");

您現在可以手動創建,甚至可以以編程方式創建。 如果你對如何實現這一點感到好奇,你可以閱讀這篇文章,我寫了一篇關於node.js的Nano - Minimalistic CouchDB

有關更多信息,請參閱expressjsnano 希望這可以幫助!

您可以使用諸如Cradle之類的 node.js模塊來使用CouchDB。

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

只需發出HTTP請求。 我會建議request

這是我的代碼中的一個例子

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

這是我的代碼中的另一個例子

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

我有一個模塊( node-couchdb-api )我是為了這個目的編寫的。 它沒有ORM或其他類似的功能,它只是CouchDB提供的HTTP API的簡單包裝器。 它甚至遵循Node.JS為異步回調建立的約定,使您的代碼更加一致。 :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM