forked from bbonnin/foundationdb-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
56 lines (48 loc) · 1.41 KB
/
app.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
50
51
52
53
54
55
56
var express = require("express");
var http = require("http");
var qs = require("querystring");
var fdb = require('fdb').apiVersion(22);
var db;
//***********************************************
// Express configuration
//***********************************************
var app = express();
var httpServer = http.createServer(app);
app.configure(function() {
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.static(__dirname + '/public'));
});
//***********************************************
// FoundationDB configuration
//***********************************************
other = fdb.open(null, 'DB', function(err, opendb) {
if (!err) {
db = opendb;
}
});
//***********************************************
// Requests processing
//***********************************************
function findByKey(req, resp) {
var key = req.params.key;
var value = db[key];
if (value != null) {
resp.send({ key : key, value : value });
}
else {
resp.send(404, { error : "No value" });
}
}
function insertValue(req, resp) {
var data = req.body;
db[data.key] = data.value;
resp.send(204);
}
//***********************************************
// REST API
//***********************************************
app.get("/fdb/:key", findByKey);
app.post("/fdb", insertValue);
httpServer.listen(8888);
console.log("Listening on port 8888...");