Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
112 lines (96 loc) · 3.32 KB
/
Copy pathindex.js
File metadata and controls
112 lines (96 loc) · 3.32 KB
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
const express = require("express");//IMPORTING EXPRESS
const app = express();//USING EXPRESS THROUGH THE APP VARIABLE
const bodyParser = require("body-parser");//IMPORTING BODY-PARSER
const connection = require("./database/database");//IMPORTING CONNECTION WITH DB
const categoriesController = require("./categories/CategoriesController");//IMPORTING CATEGORIES CONTROLLERS
const articlesController = require("./articles/ArticlesController");//IMPORTING ARTICLES CONTROLLERS
const usersController = require("./users/UsersController")//IMPORTING USERS CONTROLLERS
const Article = require("./articles/Article");//IMPORTING THE ARTICLE MODEL
const Category = require("./categories/Category");//IMPORTING THE CATEGORY MODEL
const User = require("./users/User")//IMPORTING THE USER MODEL
const res = require("express/lib/response");
const session = require("express-session")//IMPORTING EXPRESS SESSION TO BUILD LOGIN SYSTEM
//SESSIONS
app.use(session({
secret: 'qualquercoisa',
cookie: { maxAge: 30000 },
resave: false,
saveUninitialized: false
}));
// View engine
app.set('view engine','ejs');//SETTING THE VIEW ENGINE AS EJS
// Static
app.use(express.static('public'));//SETTING PUBLIC AS THE FOLDER TO SAVE STATIC FILES(CSS,IMAGES,JS)
//Body parser
//BODY PARSER IS RESPONSIBLE FOR DECODIFICATING THE DATA THAT COMES FROM FORMS INTO A READABLE JAVASCRIPT STRUCTURE FOR THE BACK-END
app.use(bodyParser.urlencoded({extended: false}));//USING BODY-PARSER
app.use(bodyParser.json());//BODY-PARSER FOR API'S
// Database(PROMISE STRUCTURE TO VALIDATE CONNECTION WITH DATABASE)
connection
.authenticate()
.then(() => {
console.log("");
}).catch((error) => {
console.log(error);
})
app.use("/",categoriesController);//USING THE CATEGORIES CONTROLLERS
app.use("/",articlesController);//USING THE ARTICLES CONTROLLERS
app.use("/",usersController)//USING THE USERS CONTROLLERS
//ROUTE(/)=>INITAL ROUTE FROM THE APPLICATION
app.get("/", (req, res) => {
Article.findAll({
order:[
['id','DESC']
]
}).then(articles=>{
Category.findAll({
}).then(categories=>{
res.render("index",{articles:articles,categories:categories})
})
})
})
//SEARCHING AN ARTICLE THROUGH THE SLUG PARAMETER
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("/")
})
})
//DINAMIC NAVBAR
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("/")
})
})
//INITIALIZING THE SERVER IN THE 1700 PORT
app.listen(1700,() => {
console.log("O servidor está rodando!")
})