-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
61 lines (55 loc) · 1.47 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
const express = require("express");
const app = express();
const mongoose = require("mongoose");
const dotenv = require("dotenv");
dotenv.config();
app.use(express.urlencoded({ extended: true }));
const port = process.env.PORT;
const TodoTask = require("./models/TodoTask");
mongoose.connect(process.env.MONGO_URL, { useNewUrlParser: true }, () => {});
app.set("view engine", "ejs");
app.listen(port, () => {
console.log(`Server is running and listening to port ${port}`);
});
// GET METHOD
app.get("/home", (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,
});
console.log(todoTask);
try {
await todoTask.save();
res.redirect("/home");
} catch (err) {
res.redirect("/home");
}
});
//UPDATE
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("/home");
});
});
//delete
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("/home");
});
});