0

I have been trying to connect to couchDB to get some data back or do anything with it. I have not been able to connect to couchDB!

Please can anyone tell what am I doing wrong. Please!!!

When run this program it console logs "incoming requests" & "Got it" but nothing else.

Please just tell me how to connect to couchDB.

   var restify = require("restify");
   var server = restify.createServer();

   server.listen(8080, function(){
   console.log("incoming requests");
   });

   server.get('/users', function(req, res){

   var request = require('request');
   request('http://domain.iriscouch.com/testbook/_all_docs', function (error, response, body) {

   if (!error && response.statusCode == 200) {
   console.log(body);
   }

   });

     console.log("Got it!"); 
     res.end();
  });
Skywalker
  • 4,984
  • 16
  • 57
  • 122
  • I can't see where you are trying to actually connect to couchdb. If you install node-couchdb and follow these docs (https://www.npmjs.com/package/node-couchdb) surely it will work. – swifty Feb 20 '15 at 08:59
  • @swifty when i say connect I mean access couchDB databases. I am access it in the "server.get" function. Could you please provide an example on how to et data or post to couchdb. – Skywalker Feb 20 '15 at 09:39
  • I'm sorry, but you have never connected to couchDB. server.get is NOT connecting to your instance of couch, it is creating a route. Give min a minute and i'll post an attempted answer – swifty Feb 20 '15 at 10:38
  • @swifty If I change "5984" to 8080 I get the following error **Problem with HTTP GET request:** http.clientRequest error for request in 240~245 ms smk.iriscouch.com:8080/testbook/_all_docs and another error please see comment in your answer below. – Skywalker Feb 20 '15 at 14:29

1 Answers1

0

I think what you are after is something like the snippet below. (you will have to run npm install --save node-couchdb if you haven't already).

var restify = require("restify");
var server = restify.createServer();
var nodeCouchDB = require("node-couchdb");
var couch = new nodeCouchDB("localhost", 5984);

server.listen(8080, function(){
    console.log("incoming requests");
});

server.get('/users', function(req, res){

  //Fetch document by its id
  couch.get("databaseName", "_all_docs", function (err, resData) {
     if (err) {
        return console.error(err);
     }

     console.log(resData);
  });

  console.log("Got it!"); 
  res.end();

});

N.B. You will have to change 'databaseName', 'localhost' and 5984 as appropriate for your environment.

To insert/update/delete it all documented at https://www.npmjs.com/package/node-couchdb

swifty
  • 1,127
  • 1
  • 10
  • 23
  • Thank you for the reply. I placed my couchDB base url in localhost (smk.iriscouch.com (without the http)) and I left 5984 as it is. And I put the "testbook" as the database name and left "_all_docs" as it is. but when I run it I get this error **Problem with HTTP GET request: Socket timeout in 5003~5008 ms** http://smk.iriscouch.com:5984/testbook/_all_docs – Skywalker Feb 20 '15 at 11:04
  • If I change **"5984"** to **8080** I get the following error **Problem with HTTP GET request: http.clientRequest error for request in 240~245 ms** http://smk.iriscouch.com:8080/testbook/_all_docs – Skywalker Feb 20 '15 at 11:13