-
Notifications
You must be signed in to change notification settings - Fork 0
/
blog.js
76 lines (62 loc) · 2.19 KB
/
blog.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
function blog(req, res) {
var db = require('./dbauth.js')();
console.log("Database connection established");
// Blog front page - show latest articles
if (req.path === '/') {
// Show articles where visible=1 in reverse ID order
db.collection('articles').find({'visible' : 1}).sort({ '_id' : -1}, function(err, listOfArticles) {
if (err) {
console.log("Database Error: ", err);
res.writeHead(500, {"Content-Type" : "text/plain"});
res.end("500 error: " + err);
return;
}
var fixedList = [];
listOfArticles.forEach(function(element) {
var fixedArticle = require('ent').decode(element.article);
element.article = fixedArticle;
fixedList.push(element);
});
res.render('layout', {
title: "Leon Zaruvinsky's Blog",
adj: "blog",
articles: listOfArticles,
partials: {
header: "header",
main: "blog",
}
});
});
// Article page
} else {
var myPath = (req.path).substring(1); // strip leading slash
db.collection('articles').findOne({ path : myPath }, function(err, result) {
if (err) {
console.log("Database Error: ", err);
res.writeHead(500, {"Content-Type" : "text/plain"});
res.end("Database error: " + err);
return;
}
if (result === null) {
console.log("Article not found for request path: " + myPath);
res.writeHead(404, {"Content-Type" : "text/plain"});
res.write("404 Error:\n");
res.end("Article not found for request path: " + myPath);
return;
}
// re-encode HTML entities
var fixedArticle = require('ent').decode(result.article);
res.render('layout', {
title: result.title + " - Leon Zaruvinsky",
adjs: "pages",
article: result,
content: fixedArticle,
partials: {
header: "header",
main: "blog-article",
}
});
});
}
}
module.exports = blog