-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
110 lines (94 loc) · 2.47 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
const express = require("express");
const app = express();
const bodyParser = require("body-parser");
const session = require("express-session");
const connection = require("./database/database");
const categoriesController = require("./categories/CategoriesController");
const articlesController = require("./articles/ArticlesController");
const usersController = require("./users/UsersController");
const Article = require("./articles/Article");
const Category = require("./categories/Category");
const User = require("./users/User");
//* view engine configuration
app.set("view engine", "ejs");
//* Session
app.use(session({
secret: "y3kd40w_@$%kkiiesds",
cookie: { maxAge: 30000}
}))
//* Body Parser
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());
//* static
app.use(express.static("public"));
//* database
connection
.authenticate()
.then(() => {
console.log("Banco conectado!");
})
.catch((err) => {
console.log(err);
});
//* routes
app.use("/", categoriesController);
app.use("/", articlesController);
app.use("/", usersController);
app.get("/", (req, res) => {
Article.findAll({
order: [["id", "DESC"]],
limit: 4,
}).then((articles) => {
Category.findAll().then((categories) => {
res.render("index", {articles: articles, categories: categories});
});
});
});
app.get("/:slug", (req, res) => {
const slug = req.params.slug;
Article.findOne({
where: {
slug: slug,
},
})
.then((article) => {
// article !== undefined ? res.render('article', {article: article}) : res.redirect('/')
if (article !== undefined) {
Category.findAll().then((categories) => {
res.render("article", {article: article, categories: categories});
});
} else {
res.redirect("/");
}
})
.catch((err) => res.redirect("/"));
});
app.get("/category/:slug", (req, res) => {
const slug = req.params.slug;
Category.findOne({
where: {
slug: slug,
},
include: [{model: Article}],
})
.then((category) => {
if (category != undefined) {
Category.findAll().then((categories) => {
res.render("index", {
articles: category.articles,
categories: categories,
});
});
} else {
res.redirect("/");
}
})
.catch((err) => {
console.log(err);
res.redirect("/");
});
});
//* listen
app.listen(8080, () => {
console.log("App rodando na porta 8080...");
});