Permalink
Browse files

Updated example application to work with couchnode 0.1.0.

Change-Id: Ia952f9b76421b12c06294ecc2a3a5b50e4182c88
Reviewed-on: http://review.couchbase.org/28741
Tested-by: Brett Lawson <brett19@gmail.com>
Reviewed-by: Mark Nunberg <mnunberg@haskalah.org>
  • Loading branch information...
brett19 committed Sep 2, 2013
1 parent a61e0d7 commit d5a8a53afa6893e1bd6098b283af93e9a5481747
Showing with 38 additions and 36 deletions.
  1. +38 −36 example.js
View
@@ -1,47 +1,49 @@
-var couchbase = require("couchbase");
-var http = require("http");
-var fs = require("fs");
-var util = require("util");
+var couchbase = require("./lib/couchbase.js"),
+ http = require("http"),
+ fs = require("fs"),
+ util = require("util");
var port = 8080;
var configFilename = 'config.json';
if (fs.existsSync(configFilename)) {
- config = JSON.parse(fs.readFileSync(configFilename));
+ config = JSON.parse(fs.readFileSync(configFilename));
@SkeLLLa

SkeLLLa Sep 4, 2013

Why are you using JSON.parse(fs.readFileSync(configFilename))? Is it better than just config = require(configFilename)?

} else {
- console.log(configFilename + " not found. Using default");
- config = { };
+ console.log(configFilename + " not found. Using default");
+ config = { };
}
-couchbase.connect(config, function(err, bucket) {
- if (err) {
- // For some reason we failed to make a connection to the
- // Couchbase cluster.
- throw err;
+bucket = new couchbase.Connection(config, function(err) {
+ if (err) {
+ // For some reason we failed to make a connection to the
+ // Couchbase cluster.
+ throw err;
+ }
+});
+
+http.createServer(function(req, resp) {
+ bucket.get("hitcount", function(err, result) {
+ var doc = result.value;
+ if (!doc) {
+ doc = {count:0};
}
- http.createServer(function(req, resp) {
- bucket.get("hitcount", function(err, doc, meta) {
- // @todo check the error reason!
- if (!doc) {
- doc = {count:0};
- }
+ doc.count++;
+
+ resp.writeHead(200);
+ console.log("hits", doc.count);
- doc.count++;
- resp.writeHead(200);
- console.log("hits", doc.count);
- bucket.set("hitcount", doc, meta, function(err) {
- if (err) {
- console.warn("Failed to store hit counter: " +
- util.inspect(err));
- resp.end("<p>Internal server error. " +
- "Failed to store:</p><pre>" + util.inspect(err) +
- "</pre>");
- } else {
- resp.end('<p>The server has seen '+doc.count+' hits</p>');
- }
- })
- })
- }).listen(port);
- console.log("listening on http://localhost:" + port);
-})
+ bucket.set("hitcount", doc, {}, function(err) {
+ if (err) {
+ console.warn("Failed to store hit counter: " +
+ util.inspect(err));
+ resp.end("<p>Internal server error. " +
+ "Failed to store:</p><pre>" + util.inspect(err) +
+ "</pre>");
+ } else {
+ resp.end('<p>The server has seen '+doc.count+' hits</p>');
+ }
+ });
+ })
+}).listen(port);
+console.log("listening on http://localhost:" + port);

0 comments on commit d5a8a53

Please sign in to comment.