forked from couchbase/couchnode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.js
49 lines (42 loc) · 1.25 KB
/
example.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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));
} else {
console.log(configFilename + " not found. Using default");
config = { };
}
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};
}
doc.count++;
resp.writeHead(200);
console.log("hits", doc.count);
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);