如何连接CouchDb和HapiJs?

如何连接CouchDb和HapiJs?,couchdb,hapijs,Couchdb,Hapijs,我有一个应用程序是使用HapiJs framework for Node编写的,我想将它连接到CouchDb数据库,但我很难找到这样做的代码 有人能帮我写代码吗?这样做的“正常”方式是什么 干杯 couchdb不需要任何框架。一切都可以通过RESTAPI获得。只需使用模块向api发出请求。例如: 阅读文档 request.get("http://localhost:5984/name_of_db/id_of_docuement", function(err,res,data){

我有一个应用程序是使用HapiJs framework for Node编写的,我想将它连接到CouchDb数据库,但我很难找到这样做的代码

有人能帮我写代码吗?这样做的“正常”方式是什么


干杯

couchdb不需要任何框架。一切都可以通过RESTAPI获得。只需使用模块向api发出请求。例如:

阅读文档

 request.get("http://localhost:5984/name_of_db/id_of_docuement",
        function(err,res,data){
      if(err) console.log(err);  
     console.log(data);
 });
从视图中读取

    request.get(
    "http://localhost:5984/name_of_db/_design/d_name/_view/_view_name",
        function(err,res,data){
      if(err) console.log(err);  
     console.log(data);
 });
整个api都有文档记录

您不需要管理连接,也不需要处理打开和关闭数据库的操作,而您可能需要处理其他数据库。只需启动couchdb并开始从应用程序中请求

但是,如果您发现直接向api发出请求对您来说有点麻烦,那么您可以尝试使用它,它为使用couchdb进行操作提供了更好的语法

一些代码片段

 request.get("http://localhost:5984/name_of_db/id_of_docuement",
        function(err,res,data){
      if(err) console.log(err);  
     console.log(data);
 });
好吧,我不熟悉hapi,所以我会告诉你如何按要求做

请从下面的示例中考虑此示例


当您调用
/
端点时,将执行该端点的请求处理程序。它向couchdb端点发出请求以获取文档。除此之外,您不需要任何东西来连接couchdb。

另一个选项可以是hapi couchdb插件()

使用它比直接调用coach API更“像hapi”

以下是插件文档中的一个示例:

var Hapi = require('hapi'),
    server = new Hapi.Server();

server.connection({
    host: '0.0.0.0',
    port: 8080
});

// Register plugin with some options
server.register({
    plugin: require('hapi-couchdb'),
    options: {
        url: 'http://username:password@localhost:5984',
        db: 'mycouchdb'
    }
}, function (err) {
    if(err) {
        console.log('Error registering hapi-couchdb', err);
    } else {
        console.log('hapi-couchdb registered');
    }
});

// Example of accessing CouchDb within a route handler
server.route({
    method: 'GET',
    path: '/',
    handler: function (request, reply) {
        var CouchDb = request.server.plugins['hapi-couchdb'];
        // Get a document from the db
        CouchDb.Db.get('rabbit', { revs_info: true }, function(err, body) {
            if (err) {
                throw new Error(CouchDb.Error(error); // Using error decoration convenience method
            } else {
                reply(body);
            });
    }
});

server.start(function() {
    console.log('Server running on host: ' + server.info.uri);
});

好的,但我想知道的是,将我的应用程序连接到Couchdb数据库的代码是什么?就像我们写
var Hapi=require('Hapi')