-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
98 lines (73 loc) · 2.22 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
var config = require('./config.json');
var page = require('./lib/page.js');
var cons = require('consolidate');
var path = require('path');
var _ = require('underscore');
var express = require('express');
var app = express();
var uploadsDir = 'uploads';
var themesDir = 'theme';
// set up cons and hogan templating
app.engine('html', cons.hogan);
app.set('view engine', 'html');
app.set('views', path.join(__dirname, themesDir, 'templates'));
page.setOptions({pagesDir: path.join(__dirname, 'pages')});
// routes
app.get('/favicon.ico', function(req, res){
res.sendfile(path.join(__dirname, 'favicon.ico'));
});
function render(res, err, template, model){
if(err)
return res.send(err, template);
res.render(template, model);
}
app.get('/', function(req, res){
page.pages(0, _.partial(render, res));
});
app.get('/all', function(req, res){
page.listAll(_.partial(render, res));
});
app.get('/pages/:num', function(req, res){
page.pages(req.params.num, _.partial(render, res));
});
// get /search?q=term
app.get('/search*', function(req, res){
res.send('Search for "' + req.query.q + '"');
});
app.get('/rss', function(req, res){
page.rss(function(err, template, model){
if(err)
return res.send(err, template);
res.type('application/xml');
res.render(template, model);
});
});
app.get('/tags', function(req, res){
page.tags(_.partial(render, res));
});
app.get('/tag/:tag', function(req,res){
page.tag(req.params.tag, _.partial(render, res));
});
app.get('/uploads/*', function(req, res){
var file = req.params[0];
res.sendfile(path.join(__dirname, uploadsDir, file));
}
);
app.get('/static/*', function(req, res){
var file = req.params[0];
res.sendfile(path.join(__dirname, themesDir, file));
});
app.get('/:slug', function(req, res){
page.page('', req.params.slug, _.partial(render, res));
});
// GET /some/long/path/to/file
app.get('/*/:path', function(req, res){
var path = req.params.path;
var slug = req.params[0];
page.page(path, slug, _.partial(render, res));
});
page.populateCache();
page.watchCache();
var port = config.port || 80;
app.listen(port);
console.log('Serving "' + config.site + '" on port ' + port);