forked from wicoady1/redissimulation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
54 lines (42 loc) · 1.76 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
// Import and Execute Express App
const express = require('express');
const app = express();
// Import and Execute Redis
const redis = require("redis");
const { promisify } = require('util');
const client = redis.createClient();
// Since Node Redis is Asynchronous, need to promify the client.get() hook
const getAsync = promisify(client.get).bind(client);
// Set the Express app to receive data from req.body
app.use(express.urlencoded({ extended: true }));
// Error handling if redis client encounters error
client.on("error", function (err) {
console.log("Error " + err);
});
// Informing the log that client is now connected
client.on("connect", function () {
console.log(`Initializing Redis Server on port : 6379`);
});
// Set the templating engine for Express. In this case use ejs
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
// GET / handler : render index.html inside views/
app.get('/', (req, res) => res.render('index', { title: 'ejs' }));
// POST /redis handler
app.post('/redis', async (req, res) => {
// Use the passed param 'test' into key string here
let key = `anti-spam:${req.body.test}`;
// Set value using async await function. Getting the key from redis client
let value = await getAsync(key);
console.log(`Value of the Key : ${value}`);
// If the value is existing, then response error
if (value) {
return res.status(500).send("Value Exists, Please Wait");
}
// If there is no redis key value, set new key with 10 second expiration time
console.log('Now setting the key to the redis client...');
client.set(key, key, 'EX', 10, () => {
return res.status(200).send("Status: OK");
});
});
app.listen(3000, () => console.log('Redis test App started on port: 3000!'));