forked from intel-iot-devkit/intel-iot-examples-datastore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
55 lines (47 loc) · 1.37 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
"use strict";
var PORT,
AUTH_TOKEN,
HOST,
REDIS_URL,
REDIS_OPTS,
REDIS_PORT;
var settings = require("./settings"),
redis = require("./redis"),
http = require("./http");
// get counter value
http.get("/counter/:key", function(req, res, next) {
redis.hget("counters", req.params.key, function(err, value) {
if (err) { return next(err); }
res.json({ value: +value });
});
});
// increment/create a new counter
http.get("/counter/:key/inc", function(req, res, next) {
redis.hincrby("counters", req.params.key, 1, function(err, value) {
if (err) { return next(err); }
res.json({ value: value });
});
});
// get last value from log
http.get("/logger/:key", function(req, res, next) {
redis.lindex(req.params.key, 0, function(err, value) {
if (err) { return next(err); }
res.json({ value: value });
});
});
// append value to log
http.put("/logger/:key", function(req, res, next) {
redis.lpush(req.params.key, req.body.value, function(err, length) {
if (err) { return next(err); }
res.json({ value: req.body.value });
});
});
// get all log values
http.get("/logger/:key/all", function(req, res, next) {
redis.lrange(req.params.key, 0, -1, function(err, value) {
if (err) { return next(err); }
res.json({ value: value });
});
});
// start HTTP server once connected to Redis
redis.once("ready", http.listen);