-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
109 lines (91 loc) · 2.19 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
const express = require("express");
const bodyParse = 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");
const app = express();
// View engine
app.set('view engine', 'ejs');
//End
//Session
app.use(session({
secret: "#%asdDE&*=%tsg23@#FS",
cookie: {
maxAge: 30000
}
}));
//End
// Static
app.use(express.static('public'));
//End
// Body Parse
app.use(bodyParse.urlencoded({extended: false}));
app.use(bodyParse.json());
//End
// Database
connection.authenticate().then(() => {
console.log("Connection DB OK");
}).catch((error) => {
console.log(`Erro in connection DB... Details:${error}`);
})
//End
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) => {
let slug = req.params.slug;
Article.findOne({
where: {
slug: slug
}
}).then( article => {
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) => {
let 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 => {
res.redirect("/");
})
});
app.listen(8080, () => {
console.log("The server has been started....")
});