-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
427 lines (367 loc) · 12.2 KB
/
server.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/*********************************************************************************
* WEB322 – Assignment 06
* I declare that this assignment is my own work in accordance with Seneca Academic Policy. No part
* of this assignment has been copied manually or electronically from any other source
* (including 3rd party web sites) or distributed to other students.
*
* Name: Rachit Chawla Student ID: 162759211 Date: 17th April, 2023
*
* Cyclic Web App URL: https://itchy-red-millipede.cyclic.app
*
* GitHub Repository URL: https://github.com/Rachit1313/web322-app
*
********************************************************************************/
var path = require('path');
var express = require("express");
var blog = require('./blog-service.js');
var authData = require('./auth-service.js');
var clientSessions = require("client-sessions");
var app = express();
const multer = require("multer");
const cloudinary = require('cloudinary').v2
const streamifier = require('streamifier');
const exphbs = require("express-handlebars");
const stripJs = require('strip-js');
cloudinary.config({
cloud_name: 'dd1arp27e',
api_key: '772393949359259',
api_secret: 'pj5emzg-4RXqAuwmuWzMZSCoGi0',
secure: true
});
app.use(express.static("public"));
app.use(clientSessions({
cookieName: "session", // this is the object name that will be added to 'req'
secret: "web322blogapplication", // this should be a long un-guessable string.
duration: 2 * 60 * 1000, // duration of the session in milliseconds (2 minutes)
activeDuration: 1000 * 60 // the session will be extended by this many ms each request (1 minute)
}));
app.use(function(req, res, next) {
res.locals.session = req.session;
next();
});
function ensureLogin(req, res, next) {
if (!req.session.user) {
res.redirect("/login");
} else {
next();
}
}
app.use(function (req, res, next) {
let route = req.path.substring(1);
app.locals.activeRoute =
"/" +
(isNaN(route.split("/")[1])
? route.replace(/\/(?!.*)/, "")
: route.replace(/\/(.*)/, ""));
app.locals.viewingCategory = req.query.category;
next();
});
app.use(express.urlencoded({extended: true}));
app.engine('.hbs', exphbs.engine({ extname: '.hbs',helpers: {
navLink: function(url, options){
return '<li' +
((url == app.locals.activeRoute) ? ' class="active" ' : '') +
'><a href="' + url + '">' + options.fn(this) + '</a></li>';
},
equal: function (lvalue, rvalue, options) {
if (arguments.length < 3)
throw new Error("Handlebars Helper equal needs 2 parameters");
if (lvalue != rvalue) {
return options.inverse(this);
} else {
return options.fn(this);
}
},
safeHTML: function(context){
return stripJs(context);
},
formatDate: function(dateObj){
let year = dateObj.getFullYear();
let month = (dateObj.getMonth() + 1).toString();
let day = dateObj.getDate().toString();
return `${year}-${month.padStart(2, '0')}-${day.padStart(2,'0')}`;
}
}}));
app.set('view engine', '.hbs');
app.use(function(req,res,next){
let route = req.path.substring(1);
app.locals.activeRoute = "/" + (isNaN(route.split('/')[1]) ? route.replace(/\/(?!.*)/, "") : route.replace(/\/(.*)/, ""));
app.locals.viewingCategory = req.query.category;
next();
});
const upload = multer(); // no { storage: storage } since we are not using disk storage
var HTTP_PORT = process.env.PORT || 8080;
// call this function after the http server starts listening for requests
function onHttpStart() {
console.log("Express http server listening on: " + HTTP_PORT);
}
app.use(express.static('public'));
// setup a 'route' to listen on the default url path (http://localhost)
app.get("/", function(req,res){
res.redirect('/blog');
});
// setup another route to listen on /about
app.get("/about", function(req,res){
// res.sendFile(path.join(__dirname+'/views/about.html'));
res.render('about');
});
//setup get route for /posts/add
app.get('/posts/add',ensureLogin, function(req, res) {
blog.getCategories()
.then((categories) => {
res.render("addPost", { categories: categories });
})
.catch(() => {
res.render("addPost", { categories: [] });
});
});
//setup post route for /posts/add
app.post("/posts/add", ensureLogin,upload.single("featureImage"),function(req,res){
// res.sendFile(path.join(__dirname+'/views/addPost.html'));
if (req.file) {
let streamUpload = (req) => {
return new Promise((resolve, reject) => {
let stream = cloudinary.uploader.upload_stream((error, result) => {
if (result) {
resolve(result);
} else {
reject(error);
}
});
streamifier.createReadStream(req.file.buffer).pipe(stream);
});
};
async function upload(req) {
let result = await streamUpload(req);
console.log(result);
return result;
}
upload(req).then((uploaded) => {
processPost(uploaded.url);
});
} else {
processPost("");
}
function processPost(imageUrl) {
req.body.featureImage = imageUrl;
// TODO: Process the req.body and add it as a new Blog Post before redirecting to /posts)
blog.addPost(req.body).then(() =>{
res.redirect("/posts");
});
}
});
app.get('/blog', async (req, res) => {
// Declare an object to store properties for the view
let viewData = {};
try{
// declare empty array to hold "post" objects
let posts = [];
// if there's a "category" query, filter the returned posts by category
if(req.query.category){
// Obtain the published "posts" by category
posts = await blog.getPublishedPostsByCategory(req.query.category);
}else{
// Obtain the published "posts"
posts = await blog.getPublishedPosts();
}
// sort the published posts by postDate
posts.sort((a,b) => new Date(b.postDate) - new Date(a.postDate));
// get the latest post from the front of the list (element 0)
let post = posts[0];
// store the "posts" and "post" data in the viewData object (to be passed to the view)
viewData.posts = posts;
viewData.post = post;
}catch(err){
viewData.message = "no results";
}
try{
// Obtain the full list of "categories"
let categories = await blog.getCategories();
// store the "categories" data in the viewData object (to be passed to the view)
viewData.categories = categories;
}catch(err){
viewData.categoriesMessage = "no results"
}
// render the "blog" view with all of the data (viewData)
res.render("blog", {data: viewData})
});
app.get("/blog/:id", async (req, res) => {
// Declare an object to store properties for the view
let viewData = {};
try {
// declare empty array to hold "post" objects
let posts = [];
// if there's a "category" query, filter the returned posts by category
if (req.query.category) {
// Obtain the published "posts" by category
posts = await blog.getPublishedPostsByCategory(req.query.category);
} else {
// Obtain the published "posts"
posts = await blog.getPublishedPosts();
}
// sort the published posts by postDate
posts.sort((a, b) => new Date(b.postDate) - new Date(a.postDate));
// store the "posts" and "post" data in the viewData object (to be passed to the view)
viewData.posts = posts;
} catch (err) {
viewData.message = "no results";
}
try {
// Obtain the post by "id"
let post = await blog.getPostById(req.params.id);
viewData.post = post[0]
} catch (err) {
viewData.message = "no results";
}
try {
// Obtain the full list of "categories"
let categories = await blog.getCategories();
// store the "categories" data in the viewData object (to be passed to the view)
viewData.categories = categories;
} catch (err) {
viewData.categoriesMessage = "no results"
}
// render the "blog" view with all of the data (viewData)
res.render("blog", {data: viewData})
// res.send(viewData)
});
//setup another route to listen on /posts
app.get("/posts",ensureLogin, function(req,res){
var category = req.query.category;
var minDate = req.query.minDate;
if (category) {
blog.getPostsByCategory(category).then((data) => {
if (data.length > 0){
res.render("posts", {posts: data});
}
else{
res.render("posts",{ message: "no results" });
}
}).catch((err) => {
res.render("posts", {message: "no results"});
});
}
else if (minDate) {
blog.getPostsByMinDate(minDate).then((data) => {
if (data.length > 0){
res.render("posts", {posts: data});}
else{
res.render("posts", {message: "no results"});
}
}).catch((err) =>{
res.render("posts", {message: "no results"});
});
}
else {
blog.getAllPosts().then((data) => {
if (data.length > 0){
res.render("posts", {posts: data});}
else{
res.render("posts", {message: "no results"});
}
}).catch((err) =>{
res.render("posts", {message: "no results"});
});
}
});
//setup another route to listen on /categories
app.get("/categories",ensureLogin, function(req,res){
blog.getCategories().then(function(data){
// res.json(data)
if (data.length > 0){
res.render("categories", {categories: data});
}
else{
res.render("categories",{ message: "no results" });
}
})
.catch((err)=>{
// res.status(500).send({ message: err })
res.render("categories", {message: "no results"});
});
});
app.get('/post/:value',ensureLogin, (req, res) => {
var value = req.params.value;
blog.getPostById(value).then((data) => {
res.send(data);
}).catch((err) => {res.send(err);});
});
// setup http server to listen on HTTP_PORT
// app.listen(HTTP_PORT, onHttpStart);
blog.initialize()
.then(authData.initialize())
.then(function(){
app.listen(HTTP_PORT, onHttpStart);
}).catch(function(err){
console.log("unable to start server: " + err);
});
app.get("/categories/add",ensureLogin, (req, res) => {
res.render('addCategory')
});
app.post("/categories/add",ensureLogin, (req, res) => {
blog.addCategory(req.body).then(() => {
res.redirect('/categories');
}).catch(() => {
console.log("Unable to Add category");
})
});
app.get("/categories/delete/:id",ensureLogin, (req, res) => {
blog.deleteCategoryById(req.params.id).then(() => {
res.redirect('/categories');
}).catch(() => {
console.log("Unable to Remove Category / Category not found)");
})
});
app.get("/posts/delete/:id",ensureLogin, (req, res) => {
blog.deletePostById(req.params.id).then(() => {
res.redirect('/posts');
}).catch(() => {
console.log("Unable to Remove Post / Post not found");
})
});
app.get("/login", (req, res) => {
res.render('login');
})
// ========== Get Register Page Route ==========
app.get("/register", (req, res) => {
res.render('register');
})
// ========== Post Login Page Route ==========
app.post("/login", (req, res) => {
req.body.userAgent = req.get('User-Agent');
authData.checkUser(req.body)
.then((user) => {
req.session.user = {
userName: user.userName,
email: user.email,
loginHistory: user.loginHistory
};
res.redirect('/posts');
})
.catch((err) => {
res.render('login', {errorMessage: err, userName: req.body.userName});
});
})
// ========== Post Register Page Route ==========
app.post("/register", (req, res) => {
authData.registerUser(req.body)
.then(() => {
res.render('register', { successMessage: 'User created' });
})
.catch((err) => {
res.render('register', { errorMessage: err, userName: req.body.userName });
});
})
// ========== Logout Route ==========
app.get("/logout", (req, res) => {
req.session.reset();
res.redirect("/");
})
// ========== User History Route ==========
app.get("/userHistory", ensureLogin, (req, res) => {
res.render("userHistory");
});
// setup route for page not found if user enters something that doesn't matches with any route
app.use((req, res) => {
res.status(404).send("Page Not Found");
});