Reputation: 521
I want to create a database in CouchDB using node.js as backend. I have an admin at my DBMS. I guess first we have to login and provide credential then create DB. I have tried some code (see below) but no luck. Can someone help me out?
Below is my code:
var request = require('request');
request.get(
'http://127.0.0.1:5984/',
function (error, response, body) {
if (!error && response.statusCode == 200) {
var couchConnectionObject = JSON.parse(body);
console.log('You are running couch DB version' + couchConnectionObject.version)
}
}
);
request.get(
'http://127.0.0.1:5984/_session',
function (error, response, body) {
if (!error && response.statusCode == 200) {
var couchConnectionObject = JSON.parse(body);
console.log(couchConnectionObject);
}
else{
console.log('----------------------------------------------------------------------');
console.log('response :' + JSON.stringify(response));
console.log('----------------------------------------------------------------------');
console.log('body :' + JSON.stringify(body));
}
}
);
request.post(
'http://127.0.0.1:5984/_session',
{
'user': 'username',
'pass': 'password',
'sendImmediately': false
},
function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log('----------------------------------------------------------------------');
var couchConnectionObject = JSON.parse(body);
console.log(couchConnectionObject);
}
else{
console.log('----------------------------------------------------------------------');
console.log('response :' + JSON.stringify(response));
console.log('----------------------------------------------------------------------');
console.log('body :' + JSON.stringify(body));
}
}
);
Upvotes: 0
Views: 1102
Reputation: 521
var request = require('request');
request.put(
{url : 'http://admin:ouchcouch@127.0.0.1:5984/newdb1'}, function(err,httpResponse,body) {
if (err) {
return console.error('Error:', err);
}
console.log('Body : ', body);
}
);
I used this code to authenticate and create a new DB in Couch Database
Upvotes: 0
Reputation: 800
Not a CouchDB expert, but this article might help you out.
Also, have you tried to npm install nano
(more info here)?
This simple line of code creates the database, according to the official doc
var nano = require('nano');
nano.db.create('alice').then((body) => {
console.log('database alice created!');
})
EDIT AFTER COMMENT
If you just want to make it the raw way it should be
request.put('http://127.0.0.1:5984/[DBNAME]', <your callback here>);
Upvotes: 1