-
Notifications
You must be signed in to change notification settings - Fork 5
/
posts.js
63 lines (43 loc) · 1.18 KB
/
posts.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
/*
This file contains the program that holds all posts and serves them out
*/
var fs = require('fs');
/*
Constructor function
*/
function Posts () {
var self = this, postsTxt, postsArray;
postsArray = require('./posts.json');
self.cache = Object.create({});
if (!postsArray || postsArray.length < 1) {
console.log('No posts were found');
process.exit();
}
// add the index page
try {
self.cache.index = fs.readFileSync('./posts/html/index.html', 'utf8');
}catch (e) {
console.log('index.html not found - please run the `compile` command first.');
}
postsArray.forEach(function (file) {
try {
var data = fs.readFileSync('./posts/html/' + file.filename + '.html', 'utf8');
self.cache[file.filename] = data;
} catch (e) {
console.log(file.filename + '.html not found - please run the `compile` command first.');
}
});
}
/*
This method serves out posts
@param: (String) page - name of the post to be fetched
*/
Posts.prototype.fetchPost = function (page) {
if (this.cache.hasOwnProperty(page)) {
return this.cache[page];
}
else {
return false;
}
};
module.exports = Posts;