forked from ajeetraina/todolist
-
Notifications
You must be signed in to change notification settings - Fork 6
/
app.js
66 lines (53 loc) · 1.42 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
const express = require('express');
const path = require('path');
const bodyParser = require('body-parser');
const port = 3000
const redis = require('redis');
var app = express();
const client = redis.createClient({ url: process.env.REDIS_URL });
client.on('connect', () => {
console.log('Connected to Redis...');
});
app.set('views'), path.join(__dirname, 'views');
app.set('view engine', 'ejs');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.use(express.static(path.join(__dirname, 'public')))
app.get('/', (req, res) => {
var title = 'A Simple Todo List App';
var counter = 0;
client.LRANGE('todo', 0, -1, (err, reply) => {
if(err){
res.send(err);
}
res.render('index', {
title: title,
todo: reply,
counter: counter
});
});
});
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('/');
});
});
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('/');
});
});
app.listen(3000, () => {
console.log('Server Started at port 3000...');
});
module.exports = app;