-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
122 lines (103 loc) · 3.28 KB
/
app.js
File metadata and controls
122 lines (103 loc) · 3.28 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
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
//Carregando módulos
const express = require("express")
const handlebars = require("express-handlebars")
const bodyParser = require("body-parser")
const mongoose = require("mongoose")
const session = require("express-session")
const flash = require("connect-flash")
require("./models/Postagem")
const Postagem = mongoose.model("postagens")
require("./models/Categoria")
const Categoria = mongoose.model("categorias")
const passport = require("passport")
require("./config/auth")(passport)
const app = express()
const admin = require("./routes/admin")
const usuarios = require("./routes/usuario")
const path = require("path")
const { response } = require("express")
//Configurando modulos
app.use(session({
secret: "cursodenode",
resave: true,
saveUninitialized: true
}))
app.use(passport.initialize())
app.use(passport.session())
app.use(flash())
app.use((req, res, next) => {
res.locals.success_msg = req.flash("success_msg")
res.locals.error_msg = req.flash("error_msg")
res.locals.error = req.flash("error")
next()
})
app.use(bodyParser.urlencoded({extended: true}))
app.use(bodyParser.json())
app.engine("handlebars", handlebars({defaultLayout: "main"}))
app.set("view engine", "handlebars")
mongoose.Promise = global.Promise;
mongoose.connect("mongodb://localhost/blogapp", {
useNewUrlParser: true,
useUnifiedTopology: true
}).then(() => {
console.log("Conectado em mongo")
}).catch((error) => {
console.log("Erro ao se conectar" + error)
})
//dizendo para o express que os arquivos estaticos estao na pasta 'public'
app.use(express.static(path.join(__dirname, "public")))
//Rotas
app.get("/", (req, res) => {
Postagem.find().lean().populate("categoria").sort({data: 'desc'}).then((postagens) => {
res.render("index", {postagens: postagens})
}).catch((error) => {
req.flash("error_msg", "Houve um erro interno.")
res.redirect("/404")
})
})
app.get("/postagem/:id", (req, res) => {
Postagem.findOne({_id: req.params.id}).lean().then((postagem) => {
if(postagem){
res.render("postagem/index", {postagem: postagem})
} else{
req.flash("error_msg", "Esta postagem não existe")
res.redirect("/")
}
}).catch((error) => {
req.flash("error_msg", "Houve um erro interno")
})
})
app.get("/categorias", (req, res) => {
Categoria.find().lean().then((categorias) => {
res.render("categorias/index", {categorias: categorias})
}).catch((error) => {
req.flash("error_msg", "Houve um erro interno ao listar as categorias...")
res.redirect("/")
})
})
app.get("/categorias/:id", (req, res) => {
Categoria.findOne({_id: req.params.id}).lean().then((categoria) => {
if(categoria) {
Postagem.find({categoria: categoria._id}).lean().then((postagens) => {
res.render("categorias/postagens", {postagens: postagens, categoria: categoria})
}).catch((error) => {
req.flash("error_msg", "Houve um erro ao listar os posts")
res.redirect("/")
})
} else {
req.flash("error_msg", "Esta categoria não existe")
res.redirect("/")
}
}).catch((error) => {
req.flash("error_msg", "Houve um erro interno ao carregar a página desta categoria.")
res.redirect("/")
})
})
app.get("/404", (req, res) => {
res.send("Erro 404!")
})
app.use("/admin", admin)
app.use("/usuarios", usuarios)
//Outros
const PORT = 8081
app.listen(PORT, () => console.log(`Servidor rodando na porta ${PORT}`))