-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
79 lines (62 loc) · 1.64 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
const express = require("express")
const mongoose = require("mongoose")
const app = express()
function handleError(error){
console.log(error)
}
// Models
const TodoTask = require("./models/TodoTask")
app.use(express.static("public"))
app.set("view engine", "ejs")
app.use(express.urlencoded({extended: true}))
// DB Config
//Test
// mongoose.connect('mongodb://db:27017', {useNewUrlParser: true}, () => {
// console.log("Connected to DB")
// //app.listen(3000, ()=> console.log("Server Up and Running"))
// })
mongoose.connect('mongodb://mongodb-svc:27017', { useNewUrlParser: true }).
catch(error => handleError(error));
app.listen(3000, ()=> console.log("Server Up and Running"))
// app.get('/', (req, res) => {
// res.render(`<h1>hello world</h1>`)
// })
app.get('/', (req, res) => {
TodoTask.find({}, (err, tasks) => {
res.render("todo.ejs", { todoTasks: tasks })
})
})
app.post('/', async(req, res) => {
const todoTask = new TodoTask({
content: req.body.content
})
try{
await todoTask.save()
console.log('success')
res.redirect("/")
} catch (err) {
res.redirect("/")
}
})
app.route("/edit/:id")
.get((req, res) => {
const id = req.params.id
TodoTask.find({}, (err, tasks) => {
res.render("todoEdit.ejs", { todoTasks: tasks, idTask: id })
})
})
.post((req, res) => {
const id = req.params.id;
TodoTask.findByIdAndUpdate(id, { content: req.body.content }, err => {
if (err) return res.send(500, err);
res.redirect("/");
})
})
app.route("/remove/:id")
.get((req, res) => {
const id = req.params.id;
TodoTask.findByIdAndRemove(id, err => {
if (err) return res.send(500, err);
res.redirect("/");
})
})