-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
129 lines (121 loc) · 4.05 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
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
123
124
125
126
127
128
129
//Carregando modules
const express = require("express");
const handlebars = require("express-handlebars");
const bodyParser = require("body-parser")
const app = express();
const path = require("path");
const admin = require("./routes/admin")
const usuarios = require("./routes/usuario")
const mongoose = require("mongoose");
const session = require("express-session")
const flash = require("connect-flash");
const passport = require("passport");
require("./models/postagens")
const Postagens = mongoose.model("postagens")
require("./models/Categoria")
const Categoria = mongoose.model("categorias")
require("./models/usuario")
const Usuario = mongoose.model("usuarios")
require("./config/auth")(passport)
const db = require("./config/db")
//Configurações
//Sessão
app.use(session({
secret: "usuarios",
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()
})
//BodyParser
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(db.mongoURI, {
useNewUrlParser: true, useUnifiedTopology: true
}).then( () => {
console.log("MongoDB conectado!");
}).catch( err => {
console.log("Houve um erro ao se conectar! " + err);
})
//Public
app.use(express.static(path.join(__dirname, "public")));
//Rotas
app.get("/", (req, res) => {
Postagens.find().populate("categoria").sort({data: "desc"}).then((postagens) => {
res.render("index", {postagens: postagens.map(postagens => postagens.toJSON())})
}).catch((err) => {
req.flash("error_msg", "Houve um erro interno")
res.redirect("/404")
})
if(req.user) {
console.log(req.user.id);
}
});
app.get('/postagens/:id', (req,res) => {
Postagens.findOne({_id: req.params.id}).then(postagens => {
if (postagens) {
const post = {
titulo: postagens.titulo,
data: postagens.data,
conteudo: postagens.conteudo
}
res.render('page/post', post)
} else {
req.flash("error_msg", "Essa postagem nao existe")
res.redirect("/")
}
}).catch(err => {
req.flash("error_msg", "Houve um erro interno" + err)
res.redirect("/")
})
})
app.get("/categorias", (req, res) => {
Categoria.find().then((categorias) => {
res.render("page/categoria", {categorias: categorias.map(categorias => categorias.toJSON())})
}).catch((err) => {
req.flash("erro_mgs", "Houve um erro ao carregar as categorias")
})
})
app.get("/categorias/:slug", (req, res) => {
Categoria.findOne({slug: req.params.slug}).then((categoria) => {
if (categoria) {
Postagens.find({categoria: categoria._id}).populate("categoria").then((postagens) => {
res.render("page/postagens", {postagens: postagens.map(postagens => postagens.toJSON()), categoria: categoria})
}).catch((err) => {
req.flash("error_msg", "Houve um erro ao listar as postagens" + err)
res.redirect("/categorias")
})
}else {
req.flash("error_msg", "Esta categoria não existe")
res.redirect("/categorias")
}
}).catch((err) => {
req.flash("error_msg", "Houve um erro ao carregar a página desta categoria" + err)
res.redirect("/")
})
})
app.get("/404", (req, res) => {
res.send("Erro GET 404!")
})
app.use("/admin", admin)
app.use("/usuarios", usuarios)
//Outros
const PORT = process.env.PORT || 3000
app.listen(PORT, () => {
console.log("Servidor rodando!");
console.log("URL: http://localhost:3000");
});