-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
51 lines (42 loc) · 1.38 KB
/
app.js
File metadata and controls
51 lines (42 loc) · 1.38 KB
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
const express = require("express")
const app = express()
const handlebars = require("express-handlebars").engine
const bodyParser = require("body-parser")
const post = require("./models/post")
app.engine("handlebars", handlebars({defaultLayout: "main"}))
app.set("view engine", "handlebars")
app.use(bodyParser.urlencoded({extended: false}))
app.use(bodyParser.json())
app.get("/", function(req, res){
res.render("primeira_pagina")
})
app.get("/consulta", function(req, res){
post.findAll().then(function(post){
res.render("consulta", {post})
}).catch(function(erro){
console.log("Erro ao carregar dados do banco: " + erro)
})
})
app.post("/cadastrar", function(req, res){
post.create({
nome: req.body.nome,
telefone: req.body.telefone,
origem: req.body.origem,
data_contato: req.body.data_contato,
observacao: req.body.observacao
}).then(function(){
res.redirect("/")
}).catch(function(erro){
res.send("Falha ao cadastrar os dados: " + erro)
})
})
app.get("/excluir/:id", function(req, res){
post.destroy({ where: { 'id': req.params.id}}).then(function(){
res.redirect("/consulta")
}).catch(function(erro){
res.send("Falha ao excluir os dados: " + erro)
})
})
app.listen(8081, function(){
console.log("Servidor ativo!")
})