-
Notifications
You must be signed in to change notification settings - Fork 0
/
todolist.js
65 lines (51 loc) · 1.53 KB
/
todolist.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
var PORT = 8080;
const express = require('express');
const session = require('express-session');
const bodyParser = require('body-parser');
const ejs = require("ejs");
const url = require('url');
const querystring = require('querystring');
const app = express();
app.use(session({secret: 'ssshhhhh',saveUninitialized: true,resave: true}));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.use( express.static( process.cwd() +'/views') );
app.set("view engine", "ejs");
var sess;
app.get('/', function(req,res) {
sess = req.session;
if ( sess.taches === undefined) {
sess.taches = [];
}
res.render('todolist2.ejs', {arr : sess.taches});
return res.end();
});
app.post('/ajout', function(req,res) {
sess = req.session;
if ( !sess.taches ) {
sess.taches = [];
}
if ( req.body.ajout_input.length > 2 ) {
sess.taches.push(req.body.ajout_input);
}
return res.redirect('/?ajout_succ=1');
});
app.post('/supprimer', function(req,res) {
sess = req.session;
if ( !sess.taches ) {
sess.taches = [];
var errval = encodeURIComponent('1');
return res.redirect('/?supp_err=' + errval);
}
let ch = req.body.suppr_input;
let index = sess.taches.indexOf(ch);
if (index > -1) {
sess.taches.splice(index, 1);
} else {
// j'envoie une erreur
var errval = encodeURIComponent('1');
return res.redirect('/?supp_err=' + errval);
}
return res.redirect('/');
});
app.listen(PORT);