-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
52 lines (43 loc) · 1.05 KB
/
index.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
const express = require('express');
const exphbs = require('express-handlebars');
const bodyParser = require('body-parser');
const app = express();
//create pre set tasks
const todos = [
{
"task": "wash car",
"done": false
}, {
"task": "clean house",
"done": false
}, {
"task": "have fun",
"done": true
}, {
"task": "pet leika",
"done": true
}
];
//boiler plates
app.engine('handlebars', exphbs({defaultLayout: 'main'}));
app.set('view engine', 'handlebars');
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());
//render the landing page with the pre-set tasks
app.get('/', (req, res) => {
res.render('index', {todos: todos});
});
//add tasks to todo list array
app.post('/', (req, res) => {
todos.push({task: req.body.todo, done: false});
res.redirect('/');
});
//mark tasks complete and move to completed task section
app.post('/mark', (req, res) => {
let task = req.body.task;
todos.forEach((todo)=>{
todo.task === task ? (todo.done = !todo.done) : undefined
});
res.redirect('/');
});
app.listen(3000);