Skip to content

Instantly share code, notes, and snippets.

@dscape
Created August 20, 2011 13:19
Show Gist options
  • Save dscape/e20560c9879efdcbc28c to your computer and use it in GitHub Desktop.
Save dscape/e20560c9879efdcbc28c to your computer and use it in GitHub Desktop.
var nano = require('nano')('http://localhost:5984');
var db_name = "test";
var db = nano.use(db_name);
function insert_doc(doc, tried) {
db.insert(doc,
function (error,http_headers,http_body) {
if(error) {
if(error.message === 'no_db_file' && tried < 1) {
// create database and retry
return nano.db.create(db_name, function () {
insert_doc(doc, tried+1);
});
}
else { return console.log(error); }
}
console.log(http_body);
});
}
insert_doc({nano: true}, 0);
var express = require('express')
, nano = require('nano')('http://localhost:5984')
, app = module.exports = express.createServer()
, db_name = "test"
, db = nano.use(db_name);
app.get("/", function(request,response) {
db.attachment.get("new", "logo.png").pipe(response);
});
app.listen(3333);
var nano = require('nano')('http://localhost:5984');
var request = require('request');
var db_name = "test";
var db = nano.use(db_name);
// {} for empty body as parameter is required but will be piped in
request.get("http://nodejs.org/logo.png").pipe(
db.attachment.insert("new", "logo.png", {}, "image/png")
);
var nano = require('nano')('http://localhost:5984');
nano.request({db: "_uuids"}, function(_,_,uuids){ console.log(uuids); });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment