forked from makers-upv/web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblog.js
54 lines (37 loc) · 1.31 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
var request = require('request');
var drive = require("drive-db")(process.env.drive.replace(/^\s+/, '').replace(/\s+$/, ''));
drive.onload = require('./driveafter');
drive.timeout = 1;
var categories = ['trabajo', 'startup', 'charla', 'blog', 'concurso', 'proyecto', 'taller', 'visita'];
// The index file
exports.index = function(req, res) {
drive.load(function(err, db){
if (err) {
return console.log(err);
}
// Load all of the articles
var articles = db.find({ published: 1 });
var hot = db.find({ hot: 1, published: 1 })[0];
// Show the main page
res.render('index', { blog: articles, categories: categories, hot: hot });
});
};
// Seen each article
exports.article = function(req, res) {
if(req.url.substr(-1) == '/' && req.url.length > 1) {
return res.redirect(301, req.url.slice(0, -1));
}
drive.load(function(err, db){
if (err) return console.log(err);
// Load the requested article and next one
var article = db.find({ id: req.params.id })[0];
var all = db.find({ published: 1 });
var next = all[all.indexOf(article) + 1] || false;
// No article found
if (!article) {
return res.status(404).render('404');
}
// There's no form inside the article
return res.render('article', { article: article, next: next });
});
};