-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
124 lines (107 loc) · 3.63 KB
/
index.js
File metadata and controls
124 lines (107 loc) · 3.63 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
const express = require("express"),
redis = require("redis"),
bodyParser = require("body-parser"),
logger = require("morgan"),
app = express();
// App configurations
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(logger("dev"));
const redisOptions = {
host: "localhost",
port: 6379
};
const client = redis.createClient(redisOptions);
client.on("connect", function () {
console.log("Redis is ready :)");
});
//Storing Strings
app.post("/storeString", function (req, res) {
client.set("name", req.body.name, function (err, reply) {
if (err) return res.status(500).send(err);
return res.status(200).send(reply);
});
});
// Get stored string
app.get("/storedString/:value", function (req, res) {
// For example localhost:3000/storedString/name
client.get(req.params.value, function (err, reply) {
if (err) return res.status(500).send(err);
return res.status(200).send(reply);
});
});
// Storing hash
app.post("/storeHash", function (req, res) {
client.hmset("hashSetName", "name", req.body.name, "lastname", req.body.lastname, "location", req.body.location, function (err, reply) {
if(err) return res.status(500).send(err);
return res.status(200).send(reply);
});
});
// Get Stored Hash
app.get("/storedHash/:hashSetName", function(req,res){
const hasthSetName = req.param("hashSetName");
client.hgetall(hasthSetName, function(err,reply){
if(err) return res.status(500).json(err);
else return res.status(200).json(reply);
});
});
// Storing List
app.post("/storeList", function(req,res){
client.rpush(["list", "name", req.body.tech1, req.body.tech2, req.body.tech3], function(err,reply){
if(err) return res.status(500).json(err);
// It will return 3
return res.status(200).json(reply);
});
});
// Get stored list
app.post("/StoredList", function(req,res){
client.lrange(req.body.listName, req.body.start, req.body.stop , function(err,reply){
if(err) return res.status(500).json(err);
return res.status(200).json(reply);
});
});
// Stroing Sets
app.post("/storeSet", function(req,res){
client.sadd(["setName", req.body.tech1, req.body.tech2, req.body.tech3], function(err,reply){
if(err) return res.status(500).json(err);
return res.status(200).json(reply);
});
});
// Get Strored Set
app.get("/storedSet/:setName", function(req,res){
client.smembers(req.param("setName"),function(err,reply){
if(err) return res.status(500).json(err);
return res.status(200).json(reply);
});
});
// Increment a value
app.post("/increment",function(req,res){
// First add a value
let watch = {};
client.set("value", req.body.value,function(err,reply){
watch.before = req.body.value;
if(err) console.log(err)
else{
// Then increment value
client.incr("value", function(err,reply){
if(err) return res.status(500).json(err);
// Then get incremented value
client.get("value", function(err,reply){
if(err) return console.log("2; " + err)
watch.after = reply;
return res.status(200).json(watch);
});
});
}
});
});
// Check Exists a value
app.post("/checkExist", function(req,res){
client.exists(req.body.value, function(err,reply){
if(reply === 1) return res.status(200).json("exists");
return res.status(404).json("not found");
});
});
app.listen(3000, function () {
console.log("app listening on port 3000");
});