-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
98 lines (87 loc) · 2.55 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
const express = require('express');
const session = require('express-session');
const bodyParser = require('body-parser');
const chalk = require('chalk');
const connection = require('./database/dbconfig');
const categoriesController = require('./categories/CategoriesController');
const articleController = require('./articles/ArticlesController');
const userController = require('./users/UsersController');
const Category = require('./categories/Category');
const Article = require('./articles/Article');
const app = express();
//setting view engine, bodyparser and static files location
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());
app.use(express.static('public'));
//end
//express-session
app.use(session({
secret: 'ds7&&Ds8',
cookie: {maxAge: 86400000}
}));
//database auth
connection.authenticate().then(()=>{
console.log(chalk.blue('Authenticated'));
}).catch(err=>{
console.log(err);
});
//ROUTES
app.use(categoriesController);
app.use(articleController);
app.use(userController);
app.get('/', (req,res)=>{
let next;
Article.findAndCountAll({
order: [['id', 'DESC']],
limit: 4,
}).then(articles=>{
if (4 >= articles.count ) {
next = false;
} else {
next = true
}
Category.findAll().then(categories=>{
res.render('index', {
articles: articles,
categories: categories,
page: 1,
hasNext: next,
showCategory: true
});
}).catch(err=>{
console.log(chalk.red(err));
})
}).catch(err=>{
console.log(chalk.red(err));
});
});
app.get('/posts/:slug', (req,res)=>{
let slug = req.params.slug;
if(slug) {
Article.findOne({
where:{
slug: slug
}
}).then(article=>{
Category.findAll({
order: [['id', 'DESC']]
}).then(categories=>{
res.render('post',{
article: article,
categories: categories
})
}).catch(err=>{
console.log(chalk.red(err));
res.redirect('/');
})
}).catch(err=>{
console.log(chalk.red(err));
});
} else {
res.redirect('/');
}
});
app.listen(3000, ()=>{
console.log(chalk.green('RUNNING'));
});