-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
62 lines (41 loc) · 1.45 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
const express = require("express");
const app = express();
const handlebars = require('express-handlebars');
const bodyParser = require('body-parser');
const Post = require('./models/Post');
// CONFIG
// TEMPLATE ENGINE
app.engine('handlebars', handlebars.engine({defaultLayout: 'main'}));
app.set('view engine', 'handlebars');
// Body Parser
app.use(bodyParser.urlencoded({extended:false}));
app.use(bodyParser.json());
//ROTAS
app.get("/", function(req,res){
Post.findAll({order: [['id', 'DESC']]}).then((posts) =>{
res.render('home',{posts:posts})
})
})
app.get('/cad', (req,res) =>{
res.render('form')
});
app.post('/add', (req,res) =>{
Post.create({
titulo: req.body.titulo,
conteudo: req.body.conteudo
}).then(() => {
res.send("Posted Sucessfully!")
}).catch((erro) => {
res.send("Error while Posting ===> " + erro)
});
});
app.post('/delete/:id', (req, res) => {
Post.destroy({where: {'id':req.params.id}}).then(()=>{
res.send("Deleted Successfully!")
}).catch((erro) => {
res.send("There is no such Post!")
})
})
app.listen(8096, function(){
console.log("Servidor Rodando na url http://localhost:8096");
});