forked from justincy/rootsdev.org-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
54 lines (42 loc) · 1.27 KB
/
app.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 express = require('express');
var request = require('request');
var jsdom = require('jsdom');
// Create express app
var app = express();
app.set('view engine', 'ejs');
app.use('/assets', express.static(__dirname + '/assets'));
// Handler for all GET requests
var wikiRoot = 'https://github.com/rootsdev/rootsdev.org-node/wiki';
app.get('*', function(req, res){
// Remove trailing slash if requesting home page
var path = req.path;
if( path == '/' ) {
path = '';
}
// Fetch associated github wiki article
request( wikiRoot + path, function( error, response, body){
if( error && response.statusCode !== 200 ) {
res.send(404);
} else {
// Parse the article with jsdom
jsdom.env({
html: body,
scripts: [
'http://code.jquery.com/jquery-1.7.min.js'
]
}, function(err, window) {
var $ = window.jQuery;
// Return the wiki article's body
res.render('template', {
wikiBody: $('#wiki-body').html(),
wikiTitle: $('#head .instapaper_title').html(),
lastUpdated: $('#last-edit time').html(),
githubLink: wikiRoot + path
});
});
}
});
});
// Bind app to port
var port = process.env.PORT || 3000;
app.listen(port);