-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
79 lines (62 loc) · 1.83 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
79
// iniciando o servidor
const express = require('express');
const app = express();
const mongoose = require('mongoose')
//conectando com o server online
const dotenv = require('dotenv')
//definindo o models
const TodoTask = require('./models/TodoTask')
dotenv.config()
//definindo css
app.use("/static", express.static("public"));
app.use(express.urlencoded({ extends: true }))
//conectando com o mongoose
//mongoose.set("useFindAndModify", false)
mongoose.connect(process.env.DB_CONNECT, { useNewUrlParser: true }, () => {
console.log('Conectado com o banco')
// definindo o numero da porta do servidor local
app.listen(process.env.PORT || 3000, () => {
console.log('Servidor funcionando!')
})
})
// configuração da views
app.set("view engine", "ejs")
//metodo get ou metodo ler
app.get('/', (req, res) => {
TodoTask.find({}, (err, tasks) => {
res.render("todo.ejs", { todoTasks: tasks })
})
})
//metodo post onde salva o arquivo
app.post('/', async (req, res) => {
const todoTask = new TodoTask({
content: req.body.content
})
try {
await todoTask.save();
res.redirect('/')
} catch (error) {
res.redirect('/')
}
})
//metodo editar
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("/");
});
});
// metodo deletar
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("/");
});
});