-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
204 lines (179 loc) · 5.04 KB
/
app.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
const express = require('express');
const app = express();
const exhbs = require('express-handlebars');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const methodOverride = require('method-override');
const path = require('path');
const flash = require('connect-flash');
const session = require('express-session');
const passport = require('passport');
const handlebars = require('handlebars');
var HandlebarsIntl = require('handlebars-intl');
const port = process.env.PORT || 3000;
// Listen To Port 3000
app.listen(port, () => {
console.log(`App started at port ${port}`);
});
// Register HandlebarsIntl
HandlebarsIntl.registerWith(handlebars);
// Register Helper
handlebars.registerHelper('trimString', function(passedString, startstring, endstring) {
var theString = passedString.substring( startstring, endstring );
return new handlebars.SafeString(theString + '....')
});
// Register Helper
handlebars.registerHelper('activeClass', (param, page) => {
return param == page ? 'active' : '';
})
// Register Helper
handlebars.registerHelper('sum', (num, value) => {
return parseInt(num + value);
})
// Load Passport config
require('./config/passport')(passport);
// Connect To DB
mongoose.connect('mongodb://sera:123456m@ds249398.mlab.com:49398/sera', {
useNewUrlParser: true,
}).then(() => {
console.log(`Mongodb Connected`);
}).catch(err => {
console.log(err);
});
// Load Models
// Load Post Model
require('./models/Post')
const Post = mongoose.model('posts');
// Load Category Model
require('./models/Category');
const Category = mongoose.model('categories');
// Connect Flash Middleware
app.use(flash());
// Express Session Middleware
app.use(session({
saveUninitialized: true,
resave: true,
secret: 'secret',
}));
// Passport Middleware
app.use(passport.initialize());
app.use(passport.session());
const url = require('url')
// Set Global Variables
app.use((req, res, next) => {
res.locals.success_msg = req.flash('success_msg');
res.locals.error_msg = req.flash('error_msg');
res.locals.error = req.flash('error');
res.locals.user = req.user;
next();
});
// Check For Admin Route
app.get('/*', function(req, res, next){
const currentRoute = req.url;
if(currentRoute.split('/')[1] == 'admin' && req.user !== undefined){
res.locals.adminRoute = true;
} else {
res.locals.adminRoute = false;
}
next();
})
// Express Handlebars
app.set('view engine', 'handlebars');
app.engine('handlebars', exhbs({
defaultLayout: 'main',
}));
// Static Files
app.use('/assets', express.static(path.join(__dirname + '/public')));
// Body Parser Middleware
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
// Method Override Middleware
app.use(methodOverride('_method'));
// Load Routes
const users = require('./routes/users');
const admin = require('./routes/admin');
const posts = require('./routes/posts');
const categories = require('./routes/categories');
const single = require('./routes/single');
const comments = require('./routes/comments');
const members = require('./routes/members');
// Homepage Route
app.get('/', (req, res) => {
Category.find()
.then(categories => {
Post.find().then(allposts => {
Post.find().sort({created_at: 'desc'}).limit(9)
.then(posts => {
const count = Math.ceil(allposts.length / 9);
let pagesCount = [];
let i = 0;
for(;;){
if(i >= count){
break;
}
pagesCount.push({page: i});
i++;
}
res.render('pages/index', {
posts, count, pagesCount, pageId: '0', categories
});
});
});
})
});
app.get('/page/:id', (req, res) => {
Category.find().then(categories => {
Post.find().then(allposts => {
Post.find()
.sort({created_at: 'desc'})
.skip((parseInt(req.params.id) * 9))
.limit(9)
.then(posts => {
const count = Math.ceil(allposts.length / 9);
let pagesCount = [];
let i = 0;
for(;;){
if(i >= count){
break;
}
pagesCount.push({page: i});
i++;
}
res.render('pages/index', {
posts, count, pagesCount, categories
});
});
});
})
});
// Categories Route
app.get('/categories/:id', (req, res) => {
Category.find().then(categories => {
Post.find({
category: req.params.id
}).sort({
created_at: 'desc'
}).then(categoryPosts => {
res.render('pages/categories', {categories, categoryPosts});
});
});
});
app.get('/author/:id', (req, res) => {
Category.find().then(categories => {
Post.find({
user: req.params.id
}).sort({
created_at: 'desc'
}).then(categoryPosts => {
res.render('pages/categories', {categories, categoryPosts});
});
});
});
// Set Routes
app.use('/users', users);
app.use('/admin', admin);
app.use('/admin/posts', posts);
app.use('/admin', categories);
app.use('/post', single);
app.use('/admin/comments', comments);
app.use('/admin/users', members);