Skip to content

Instantly share code, notes, and snippets.

@vaclavbohac
Created January 22, 2011 15:23
Show Gist options
  • Save vaclavbohac/791177 to your computer and use it in GitHub Desktop.
Save vaclavbohac/791177 to your computer and use it in GitHub Desktop.
This script for nodejs tests if couchdb server is running.
/**
* Test if couchdb is live.
* possible@usage:~$ nodejs liveoncouch.js --hostname 192.168.0.32 --port 5982
* @author Vaclav Bohac (c) 2011
*/
(function (util, http) {
var getArguments = function () {
var ret = {
hostname: "127.0.0.1",
port: 5984
},
usage = "Usage:\n\t-h, --hostname server-name\n\t-p, --port port-number";
process.argv.forEach(function (value, index, argv) {
var hostname, port;
if ( value === "--hostname" || value === "-h" ) {
hostname = argv[index + 1];
if ( hostname === undefined ) {
util.puts(usage);
process.exit(1);
}
ret.hostname = hostname;
}
if ( value === "--port" || value === "-p" ) {
port = argv[index + 1];
if ( port === undefined ) {
util.puts(usage);
process.exit(1);
}
ret.port = port;
}
});
return ret;
};
var testCouch = function (port, hostname) {
var client = http.createClient(port, hostname);
client.on("error", function (err) {
util.puts("Server is not running.");
process.exit();
});
var request = client.request("GET", "/");
request.on("response", function (response) {
var responseBody = "";
response.on("data", function (chunk) {
responseBody += chunk;
});
response.on("end", function () {
var result = JSON.parse(responseBody);
if ( result.couchdb !== undefined ) {
util.puts("Server is running.");
return;
}
util.puts("Server is not running.");
});
});
request.end();
};
var s = getArguments();
testCouch(s.port, s.hostname);
}(require("util"), require("http")));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment