5

Possible Duplicate:
Getting ' bad_request invalid_json' error when trying to insert document into CouchDB from Node.js

The highest voted answer on CouchDB and Node.js - What module do you recommend? recommends not to use libraries such as nano or cradle for starting with Node.js and CouchDB.

However I haven't found any tutorial on how to perform standard operations for all DBMSes like create database, create table, add and view data etc. programmatically.

EDIT: (partial answer) after installing and starting CouchDB go to http://localhost:5984/_utils/script/couch.js.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
DSblizzard
  • 4,007
  • 7
  • 48
  • 76

4 Answers4

5

You should start by reading the CouchDB book.

No idea why you don't want to use a module: I think you took an answer out of context (an answer that is probably one year old) and made your decision not to use a module.

That is not likely to be helpful to get stuff done. :) You are just repeating work that is done, and issues that have been fixed, etc.

If you want to learn CouchDB, read the book. You can read nano's source as it maps really closely to the API and should be easy to read, but the book is the full way to go.

If by any reason you decide you still want to implement your own module to do what others already do well, go for it :)

If instead you are looking for resources on using nano there are quite a few:

Octavian Helm
  • 39,405
  • 19
  • 98
  • 102
dscape
  • 2,506
  • 1
  • 22
  • 20
  • Jason's advice is still valid regardless of the answer's age. Especially with Node.js' there's no real need for a library that just wraps the excellent HTTP API. – Octavian Helm Jul 26 '12 at 06:44
2

Thanks to Ruben Verborgh, I compiled micro-tutorial from several sources myself.

var http = require('http')
var sys = require('sys')


var couchdbPath = 'http://localhost:5984/'

request = require('request')
h = {accept: 'application/json', 'content-type': 'application/json'}
request(
  {uri: couchdbPath + '_all_dbs', headers:h}, 
  function(err, response, body) { console.log(sys.inspect(JSON.parse(body))); }
)

// add database
request(
  {uri: couchdbPath + 'dbname', method:'PUT', headers:h},
  function (err, response, body) {
    if (err)
      throw err;
    if (response.statusCode !== 201)
      throw new Error("Could not create database. " + body);
  }
)

// Modify existing document
var options = {
  host: "localhost",
  port: 5984,
  path: "/dbname",
  headers: {"content-type": "application/json"},
  method: "PUT"
};

var req = http.request(options, function(res) {
  console.log('STATUS: ' + res.statusCode);
  //console.log('HEADERS: ' + JSON.stringify(res.headers));
  res.setEncoding('utf8');
  res.on('data', function (chunk) {
    console.log('BODY: ' + chunk);
  });
});

req.on('error', function(e) {
  console.log('problem with request: ' + e.message);
});

// write data to request body
req.write(JSON.stringify({
  "_id":"rabbit",
  "_rev":"4-8cee219da7e61616b7ab22c3614b9526",
  "Subject":"I like Plankton"
}));

req.end();

I used following documentation:

Community
  • 1
  • 1
DSblizzard
  • 4,007
  • 7
  • 48
  • 76
0

CouchDB is not an SQL database engine. It's in the family of the "NoSQL" ones.

You don't do select, you don't create tables, etc.

It's completely different.

It's actually using a REST API to work. Like, to access all the documents, you access them using an HTTP GET on the following URL: http://some.server/someDbName/_all_docs

For a more thorough introduction, I suggest looking for "CouchDB tutorial" on Google.

You'll find good links like this one or this one. (I'm not vouching for any, they just look good as an introduction.)

To make an http request in node.js, you can use the request method of the built-in http module. A shortcut method is http.get, which you can use like this:

var http = require( 'http' );

http.get( 'http://some.url/with/params', function( res ) {
    // res has the values returned
});

Edit after reading your code:

Firstly, the doc you're using if outdated. Node is at v0.8, not 0.4.

Secondly, your request = require('request') must give some problems (does the module exist?). I don't think the first part is even executed.

Thirdly, just try a GET request for now. Something like:

var http = require( 'http' );
http.get( 'http://localhost:5984/_all_dbs', function( res ) {
    console.log( res );
});

See if it's working. If it is, you already know how to use couchdb ;)

Lastly, your request at the end doesn't seem wrong. Maybe it's related to require('request') though, so I don't know.

Florian Margaine
  • 58,730
  • 15
  • 91
  • 116
0

Here are a few hands-on examples, thoughts and code-snippets which should help in your study

Simple Blog with Coffeescript, Express and CoudbDB

Thoughts on development using CouchDB and Nodejs

Bind CouchDB and Node.js

Getting Started with Node.js, Express and CouchDB - this link does not seem to be accessible now, but it seems a temporary issue.

Here's one on testing CouchDB - Mock testing CouchDB using Node.js

Hope it helps.

almypal
  • 6,612
  • 4
  • 25
  • 25
  • First link - uses cradle, second - not a solution, third - uses nano. – DSblizzard Jul 25 '12 at 09:01
  • Have been compiling useful resources on Node.js for around a year now and my guess is the list is pretty much what's available on CouchDB and Node.js – almypal Jul 25 '12 at 09:24
  • almypal your list is lacking quite a few. i'll post them in a separate answer – dscape Jul 25 '12 at 17:27