-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapi_store.js
50 lines (43 loc) · 1.38 KB
/
api_store.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 store;
var log_request = require('./logger.js').log_request;
exports.init = function(engine) {
store = engine.store();
}
exports.get = function(request, response) {
var key = request.params.key;
log_request(request, "key=" + key + "...");
store.load(key, function(err, val) {
if (val != undefined) {
response.status(200);
response.json({
"key": key,
"val": val
});
log_request(request, "value for key " + key + " is: " + val);
} else {
response.status(404).json({
"error": "Value for key " + key + " not present"
});
log_request(request, "value for key " + key + " not present");
}
});
};
exports.post = function(request, response) {
var key = request.params.key;
var val = request.params.val;
log_request(request, "value posted for key " + key + " is: " + val);
store.save(key, val, function(error) {
if (error == undefined) {
response.status(201).end();
} else {
response.status(422).json({
"error": error
}).end();
}
});
}
exports.dump = function(request, response) {
console.log("\n=========== DATABASE ===========");
console.log(JSON.stringify(store.data(), null, 2), "\n");
response.status(200).end();
};