-
Notifications
You must be signed in to change notification settings - Fork 14
/
app.js
72 lines (61 loc) · 1.57 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//Declarations
const express = require('express');
const path = require('path');
const bodyParser = require('body-parser');
const redis = require('redis');
//Initialization of express
var app = express();
//Redis Client
const client = redis.createClient();
client.on('connect', () => {
console.log('Connected to Redis...');
});
//View engine
app.set('views'), path.join(__dirname, 'views');
app.set('view engine', 'ejs');
//Normalization of the elements used
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.use(express.static(path.join(__dirname, 'public')));
//This is our router
app.get('/', (req, res) => {
var title = 'Redis TODO List';
var counter = 0;
client.LRANGE('todo', 0, -1, (err, reply) => {
if(err){
res.send(err);
}
res.render('index', {
title: title,
todo: reply,
counter: counter
});
});
});
//Add messages on redis
app.post('/todo/add', (req, res, next) => {
var todo = req.body.todos;
client.RPUSH('todo', todo, (err, reply) => {
if(err){
res.send(err);
}
res.redirect('/');
});
});
//Delete messages on redis by index
app.post('/todo/delete', (req, res, next) => {
var delTODO = req.body.todo;
var deleted = '__DELETED__';
client.LRANGE('todo', 0, -1, (err, todo) => {
for(let i = 0; i < delTODO.length; i++){
client.LSET('todo', delTODO[i], deleted);
}
client.LREM('todo', 0, deleted);
res.redirect('/');
});
});
//Port listen of the app
app.listen(3000, () => {
console.log('Server Started at port 3000...');
});
module.exports = app;