-
Notifications
You must be signed in to change notification settings - Fork 1
/
web.js
62 lines (47 loc) · 1.85 KB
/
web.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
var express = require('express'),
http = require('http'),
fs = require('fs');
var walkdir = require('walkdir');
var _posts = global._posts = {};
var windows = process && process.platform == 'win32';
var postEmitter = walkdir('posts', {max_depth: 1});
// TODO Add some error handling (is it a markdown file, does a meta file exist for it?)
postEmitter.on('file', function(file, stat) {
var str = file.split(windows ? '\\posts\\' : '/posts/')[1].split('.')[0];
if(str == '') {
return;
}
var json = JSON.parse(fs.readFileSync(windows ? ('posts\\meta\\' + str + '.json') : ('posts/meta/' + str + '.json'), 'utf8'));
var content = fs.readFileSync(file, 'utf8');
json.date = new Date(json.date);
json.content = content;
_posts[str] = json;
});
postEmitter.on('end', function() {
var total = Object.keys(_posts).length;
console.log('Loaded ' + total + ' blog post' + (total == 1 ? '' : 's'));
var app = module.exports = express();
app.use('/assets', express.static('public'));
app.set('view cache');
app.set('etag', false);
app.set('port', (process.env.PORT || 3000));
app.set('view engine', 'html');
app.set('layout', 'layout');
app.set('partials', {header: 'partials/header', footer: 'partials/footer'});
app.engine('html', require('hogan-express'));
app.use(function(req, res, next) {
res.locals.system = {
// The system-wide title of your blog
title: 'Crazy Man Ramblings',
year: new Date().getFullYear(),
// Your google analytics code
analyticsCode: false
};
return next();
});
app.use('/', require('./routes'));
var appServer = app.listen(app.get('port'), function() {
console.log('Express server listening on port ' + app.get('port'));
});
appServer.setTimeout(5000);
});