-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
121 lines (112 loc) · 3.64 KB
/
app.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
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
//Carregando módulos
const express = require('express');
const handlebars = require('express-handlebars');
const bodyParser = require('body-parser');
const app = express();
const admin = require('./routes/admin');
const path = require('path');
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 usuarios = require("./routes/usuario")
const passport = require("passport")
require("./config/auth")(passport)
//Configurações
//Sessão
app.use(session({
secret: "cursodenode",
resave: true,
saveUninitialized: true
}))
app.use(passport.initialize())
app.use(passport.session())
app.use(flash());
//Middleware
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");
res.locals.user = req.user || null;
next();
});
//Body Parser
app.use(bodyParser.urlencoded({extended: true}))
app.use(bodyParser.json())
//Handlebars
app.engine('handlebars', handlebars({defaultLayout: 'main'}))
app.set('view engine', 'handlebars');
//Mongoose
mongoose.Promise = global.Promise;
mongoose.connect("mongodb://localhost/blogapp").then(() => {
console.log("Conectado ao mongo!");
}).catch((err) => {
console.log("Erro ao se conectar: " + err);
});
//Public
app.use(express.static(path.join(__dirname, "public")));
/*app.use((req, res, next) => {
console.log("Middleware!")
next();
});*/
//Rotas
app.get('/', (req, res) => {
Postagem.find().populate("categoria").sort({data: "desc"}).then((postagens) => {
res.render("index", {postagens: postagens})
}).catch((err) => {
req.flash("error_msg", "Houve um erro interno")
res.redirect("/404")
})
})
app.get("/postagem/:slug", (req, res) => {
Postagem.findOne({slug: req.params.slug}).then((postagem) => {
if(postagem){
res.render("postagem/index", {postagem: postagem})
}else{
req.flash("error_msg", "Esta postagem não existe!")
res.redirect("/")
}
}).catch((err) => {
req.flash("error_msg", "Houve um erro interno")
res.redirect("/")
})
})
app.get("/categorias", (req, res) => {
Categoria.find().then((categorias) => {
res.render("categorias/index", {categorias: categorias})
}).catch((err) => {
req.flash("error_msg", "Houve um erro interno ao listar as categorias")
res.redirect("/")
})
})
app.get("/categorias/:slug", (req, res) => {
Categoria.findOne({slug: req.params.slug}).then((categoria) => {
if(categoria){
Postagem.find({categoria: categoria._id}).then((postagens) => {
res.render("categorias/postagens", {postagens: postagens, categoria: categoria})
}).catch((err) => {
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((err) => {
req.flash("error_msg", "Houve um erro interno")
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!");
});