-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
66 lines (52 loc) · 1.52 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
const express = require('express');
const server = express();
const path = require('path');
const bodyParser = require('body-parser');
// Setando o view engine
server.engine('html', require('ejs').renderFile);
server.set('view engine', 'ejs');
// Definindo a pasta public
server.use('/public', express.static(path.join(__dirname, 'public')))
// Definindo a pasta de views
server.set('views', path.join(__dirname, '/views'));
// Configurando o body-parser
server.use(express.urlencoded({extended: true}));
server.use(express.json());
let tarefas = []
// Criando rotas para o projeto
server.get("/", (req, res)=>{
res.render('index', {tarefasList:tarefas});
});
server.get('/deletar/:id', (req, res)=>{
const index = tarefas.indexOf(req.params.id);
console.log(req.params.id)
tarefas.splice(index,1)
res.redirect('/');
});
server.post('/form', (req, res,)=>{
const {campo1} = req.body
console.log(campo1)
function erro(res){
if(res.status > 302){
console.log(res)
}
}
function criarTarefa(){
console.log(req.body)
if(campo1 != undefined && campo1.trim()){
tarefas.push(campo1)
res.redirect('/');
}else{
res.redirect('/');
return false
}
}
// if(campo1.length === 0 || !campo1.trim()){
// console.log("Deu errado")
// return false
// }
criarTarefa()
})
// Iniciando o servidor
const port = 3033;
server.listen(port, ()=> console.log("Servidor rodando"));