Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Since createClient has been deprecated, how to create a DB using Node.js now

Tags:

node.js

Earlier this was the code to create a database using Node.js:

var client = http.createClient(5984, "127.0.0.1")
var request = client.request("PUT", "/johnTest");
request.end();
request.on("response", function(response) {
    response.on("end", function() {
        if ( response.statusCode == 201 ) {
            console.log("Database successfully created.");
        } else {
            console.log("Could not create database.");
        }
    });
});

Now since createClient has been deprecated, how do we create a DB using Node.js

like image 954
Nick Div Avatar asked Mar 17 '14 19:03

Nick Div


1 Answers

var client = http.createClient(5984 /* port */, "127.0.0.1" /* host */)
var request = client.request("PUT" /* method */, "/johnTest" /* path */);

would be converted to:

var request = http.request({
    port: 5984,
    host: '127.0.0.1',
    method: 'PUT',
    path: '/johnTest'
});

Also, note, the way you are waiting for the response 'end' event will work in node v0.8.x, but will not fire in v0.10.x. I assume since you posted this code that is does actually work though, so you are on v0.8.x. If that is not the case, let me know.

like image 146
loganfsmyth Avatar answered Sep 19 '22 04:09

loganfsmyth