-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
204 lines (176 loc) · 4.94 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
/**
* We're gonna use (seen before):
*
* Express,
* Mongoose,
* Handlebars,
* Body-parser
*
* New modules:
*
* Bootstrap => HTML framework
* download > unzip > CTRL + X > CTRL + V inside the 'public' dir of our project
* Express-session:
* npm install --save express-session
* Connect-flash:
* npm install --save connect-flash
* bcryptjs:
* (allow us to 'hash' the passwords)
* npm install --save bcryptjs
* Passport-local:
* (athenticates the user using our database)
* npm install --save passport
* npm install --save passport-local
*
*/
// Requiring Modules:
const express = require('express');
const app = express();
const path = require('path');
const handlebars = require('express-handlebars');
const bodyParser = require('body-parser');
const mongoose = require ('mongoose');
const admin = require('./routes/admin');
const user = require('./routes/user');
const session = require('express-session');
const flash = require('connect-flash');
require("./models/Post");
const Post = mongoose.model('Post');
require("./models/Category");
const Category = mongoose.model('Category');
const passport = require('passport');
require("./config/auth")(passport);
const db = require('./config/db');
// Configs
// Session:
app.use(session({
secret: "password",
resave: true,
saveUninitialized: true
}));
app.use(passport.initialize());
app.use(passport.session());
// Flash:
app.use(flash());
// Middleware:
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");
// req.user is automatically created by Passport and stores the logged user data
res.locals.user = req.user || null;
//console.log("This is the Middleware.");
// NERVER FORGET the 'next()', otherside our app will be stopping here:
next();
});
// Handlebars:
app.engine('handlebars', handlebars.engine({
partialsDir: path.join(__dirname, 'views/partials'),
layoutsDir: path.join(__dirname, 'views/Layouts'),
defaultLayout: 'main',
runtimeOptions: {
allowProtoPropertiesByDefault: true,
allowProtoMethodsByDefault: false,
}
}));
app.set('view engine', 'handlebars');
app.set('views', path.join(__dirname, 'views'));
// Body-Parser:
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());
// Mongoose:
mongoose.Promise = global.Promise;
mongoose.connect(db.mongoURI, {
useNewUrlParser:true,
useUnifiedTopology: true
}).then(() => {
console.log("MongoDB connected...");
}).catch((err) => {
console.log("Error: " + err);
});
// Public:
// Static files (CSS, for example):
app.use(express.static(__dirname + '/public'));
// Routes:
app.use('/admin', admin);
app.use('/user', user);
app.get('/', (req, res) => {
Post.find().lean().populate('category').sort({
date: 'desc'
}).then((posts) => {
res.render('index', {
posts: posts
});
}).catch((err) => {
req.flash("error_msg", "Internal error!");
res.redirect('/404');
});
});
app.get('/404', (req, res) => {
res.send("Error 404");
});
app.get('/categories', (req, res) => {
Category.find().lean().then((categories) => {
res.render('categories', {
categories: categories
});
}).catch((err) => {
req.flash("error_msg", "Error listing categories!");
res.redirect('/');
});
});
app.get('/category/:slug', (req, res) => {
Category.findOne({
slug: req.params.slug
}).then((category) => {
if(category){
Post.find({
category: category._id
}).lean().then((posts) => {
res.render('postByCategory', {
category: category,
posts: posts
});
}).catch((err) => {
req.flash("error_msg", "There was an error finding posts with category.");
res.redirect('/');
});
}else{
req.flash("error_msg", "Error! Could not find this category.");
res.redirect('/');
}
}).catch((err) => {
req.flash("error_msg", "Internal error!");
res.redirect('/');
});
});
app.get('/post/:slug', (req, res) => {
Post.findOne({
slug: req.params.slug
}).then((post) => {
if(post){
Category.findOne().then((category) => {
res.render('post', {
category: category,
post: post
});
}).catch((err) => {
req.flash("error_msg", "There was an error finding the category!");
res.redirect('/');
});
}else{
req.flash("error_msg", "Error! Could not find this post.");
res.redirect('/');
}
}).catch((err) => {
req.flash("error_msg", "Internal error!");
res.redirect('/');
});
});
// Starting the server:
const PORT = process.env.PORT || 8081;
app.listen(PORT, () => {
// console.log("Server opened! Use: http://localhost:" + PORT);
console.log("Env: " + process.env.NODE_ENV);
console.log("Server opened! PORT: " + PORT);
});