-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
68 lines (52 loc) · 1.72 KB
/
server.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
const express = require('express');
const bodyparser = require('body-parser');
const app = express();
app.use(express.json());
var done = false;
let todos = [];
//add data to html page and then serve it to browser
app.use('/', express.static(__dirname + '/frontend'));
app.get('/todos', (req, res) => {
const resp = todos.map((t, i) => ({
id: i + 1,
task: t.task,
done: t.done,
due: t.due
}))
res.status(200).send(resp)
})
app.put('/todos/status', (req, res) => {
var idx = req.body.id - 1;
if (todos[idx] == null) {
return res.status(404).send({ error: 'Task with id not found.' })
}
var status = req.body.done;
var todoToChange = todos[idx];
console.log("update", todoToChange)
todoToChange.done = status;
console.log("after updating", todoToChange)
res.status(200).send({ success: 'Status updated', id: req.body.id });
})
app.post('/add', (req, res) => {
if (typeof req.body.task !== 'string' || req.body.task == '') {
return res.status(400).send({ error: 'Task name not provided' })
}
todos.push({
task: req.body.task,
done: req.body.done,
due: req.body.due
})
console.log(todos);
res.status(201).send({ success: 'New task added', id: todos.length })
})
app.post('/removedone', (req, res) => {
console.log("Before Deletion", todos);
let listOfTasks = req.body.tasklist;
for (let i = 0; i < listOfTasks.length; i++) {
// console.log(Number(listOfTasks[i]) - (i + 1))
todos.splice(Number(listOfTasks[i]) - (i + 1), 1);
}
console.log("After delete action", todos);
res.status(200).send({ sucesss: 'Removed Succesfully', id: todos.length });
})
app.listen(3000)