forked from couchbase/couchnode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.js
47 lines (42 loc) · 1.45 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
var couchbase = require("couchbase");
var http = require("http");
var fs = require("fs");
var 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 = { };
}
couchbase.connect(config, function(err, bucket) {
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, doc, meta) {
// @todo check the error reason!
if (!doc) {
doc = {count:0};
}
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);
})