-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
149 lines (136 loc) · 3.96 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
const express = require('express')
const bodyParser = require('body-parser')
const connection = require('./db/database')
const session = require('express-session')
const Article = require('./articles/Article')
const Category = require('./categories/Category')
const User = require('./user/User')
const articlesController = require('./articles/ArticlesController')
const categoriesController = require('./categories/CategoriesController')
const usersController = require('./user/UsersController')
const server = express()
const time = 60000
//VIEW ENGINE
server.set('view engine', 'ejs')
//SERVER ATRIBUTES
server.use(express.static('public'))
server.use(bodyParser.urlencoded({ extended: false }))
server.use(bodyParser.json())
server.use(session({
secret: 'b612',
cookie: {
maxAge: time*60
}
}))
//DATABASE
connection
.authenticate()
.then(() => {
console.log(`connection SUCCESS`)
})
.catch((error) => {
console.log(`connection FAILED: ${error}`)
})
//ROUTES
server.use('/', categoriesController)
server.use('/', articlesController)
server.use('/', usersController)
server.get('/', (req, res) => {
Article
.findAndCountAll({
order: [
['id', 'DESC']
],
limit: 4
})
.then((articles) => {
Category
.findAll()
.then((categories) => {
let pageExistence = articles.count > 4
res.render('index', {
articles: articles.rows,
categories,
pageExistence
})
})
.catch((err) => {
console.log(`[ERR] categories secondary-list NOT found: ${err} `)
res.render('index', {
categories: [],
articles: [],
pageExistence: false
})
})
})
.catch((err) => {
console.log(`[ERR] article list FAIL: ${err} `)
res.render('index')
})
})
server.get('/:slug', (req, res) => {
const slug = req.params.slug
Article
.findOne({
where: {
slug: slug
}
})
.then(article => {
if (article) {
Category
.findAll( {raw: true} )
.then((categories) => {
res.render('article', {
article,
categories
})
})
.catch((err) => {
console.log(`[ERR] categories secondary-list NOT found: ${err} `)
res.redirect('/')
})
} else {
res.redirect('/')
}
})
.catch((err) => {
console.log(`[ERR] specific article NOT found: ${err}`)
req.redirect('/')
})
})
server.get('/category/:slug', (req, res) => {
const slug = req.params.slug
Category
.findOne({
where: {
slug: slug
},
include: {
model: Article
}
})
.then((category) => {
if (category) {
Category
.findAll()
.then(categories => {
res.render('index', {
categories,
articles: category.articles,
pageExistence: false
})
})
} else {
res.redirect('/')
}
})
.catch((err) => {
console.log(`[ERR] articles in this category NOT found: ${err}`)
req.redirect('/')
})
})
//SERVER EXECUTION
server.listen(80, () => {
console.log('servidor iniciado')
})