Skip to content

Instantly share code, notes, and snippets.

@treffynnon
Created October 21, 2011 13:53
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save treffynnon/1303904 to your computer and use it in GitHub Desktop.
Save treffynnon/1303904 to your computer and use it in GitHub Desktop.
Connect to an IrisCouch database from Node.js, save a new document and retrieve it back again.
var http = require('http');
http.createServer(function (req, http_res) {
http_res.writeHead(200, {'Content-Type': 'text/plain'});
var response = '';
var cradle = require('cradle');
var connection = new(cradle.Connection)('https://subdomain.iriscouch.com', 443, {
auth: { username: 'username', password: 'password' }
});
var db = connection.database('database_name');
db.save('document_key', {
name: 'A Funny Name'
}, function (err, res) {
if (err) {
// Handle error
response += ' SAVE ERROR: Could not save record!!\n';
} else {
// Handle success
response += ' SUCESSFUL SAVE\n';
}
db.get('document_key', function (err, doc) {
response += ' DOCUMENT: ' + doc + '\n';
http_res.end(response);
});
});
}).listen(8071);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment