-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
97 lines (75 loc) · 2.1 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
// const bodyParser = require('body-parser');
const express = require('express');
const app = express()
const connection = require('./database/database')
const session = require('express-session')
const ModelArticles = require('./articles/modelArticles')
const ModelCategory = require('./categories/modelCategories')
const modelUser = require('./users/modelUser')
const categories = require('./categories/categoriesControl')
const Articles = require('./articles/articlesControl');
const Users = require('./users/controllerUsers')
app.set('view engine', 'ejs')
//configurando session e cookie no navegador do user
app.use(session({
secret:"@#&$*#&*&Y&*&sdfsdf", cookie:{maxAge:30000}
}))
app.use(express.static('public'))
app.use(express.urlencoded({extended:true}))
app.use(express.json())
connection
.authenticate()
.then(()=>{
console.log("Conexão com banco de dados estabelicida")
}).catch((error)=>{
console.log(error)
})
app.use("/",Articles)
app.use("/",categories)
app.use("/", Users)
app.get("/", (req, res)=>{
ModelArticles.findAll({
order:[
['id', 'DESC']
]
}).then(articles =>{
ModelCategory.findAll().then(categories =>{
res.render("index", {articles:articles, categories:categories})
})
})
})
app.get("/:slug", (req, res)=>{
var slug =req.params.slug
ModelArticles.findOne({
where:{
slug:slug
}
}).then(article =>{
if(article != undefined){
ModelCategory.findAll().then(categories =>{
res.render("article", {article:article, categories:categories})
})
}else{
res.redirect("/")
}
})
})
app.get("/category/:slug", (req, res) =>{
var slug = req.params.slug
ModelCategory.findOne({
where:{
slug:slug
},
include:[{model:ModelArticles}]
}).then( category => {
if(category != undefined){
ModelCategory.findAll().then(categories =>{
res.render("index", {articles: category.articles, categories:categories})
})
}else{
}
})
})
app.listen(3030,()=>{
console.log("servidor executando...");
})