-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
46 lines (38 loc) · 1.19 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
const express = require('express');
const bodyParser = require('body-parser');
let TodoCache = require('./models/todo/todo-cache');
let TodoItem = require('./models/todo/todo-item');
const urlencodedParser = bodyParser.urlencoded({
extended: false
});
const app = express();
let port = 8080;
let todolist = new TodoCache();
/* The to do list and the form are displayed */
app.get('/todo', function (req, res) {
res.render('todo.ejs', {
todolist: todolist.list(),
clickHandler: "func1();"
});
})
/* Adding an item to the to do list */
.post('/todo/add/', urlencodedParser, function (req, res) {
todolist.insert(new TodoItem(req.body.newtodo));
res.redirect('/todo');
})
/* Updates a item in the to do list */
.post('/todo/edit/:id', urlencodedParser, function (req, res) {
todolist.update(req.params.id, new TodoItem(req.body.editTodo));
res.redirect('/todo');
})
/* Deletes an item from the to do list */
.get('/todo/delete/:id', function (req, res) {
todolist.remove(req.params.id);
res.redirect('/todo');
})
/* Redirects to the to do list if the page requested is not found */
.use(function (req, res, next) {
res.redirect('/todo');
})
.listen(port);
console.log("Listening on port " + port);