-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathroot.js
100 lines (84 loc) · 2.73 KB
/
root.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
99
100
/* global require */
var http = require('http');
var fs = require('fs');
var path = require('path');
var express = require('express');
var path = require('path');
var app = express();
var cons = require('consolidate');
var reducedir = require('reduce-dir');
app.set('port', process.env.PORT || 3001);
app.engine('hbs', cons.handlebars);
app.set('view engine', 'hbs');
app.get('/exampletemplate', function(res, res) {
res.render('example', {
data: 'hello'
});
});
// Allow reading the files directly. express.directory and express.static
// work together to make a file browser
app.use('/raw', express.directory('api'));
app.use('/raw', express.static('api'));
// Serve static assets
app.use('/', express.static(path.join(__dirname, '/public/app')));
// Routes for serving our HTML pages
app.get('/', function(req, res) {
res.sendfile('public/app/base.html');
});
app.get('/chapters/:chapterName', function(req, res) {
res.sendfile('public/app/base.html');
});
app.get('/chapters/:chapterName/:submoduleName', function(req, res) {
res.sendfile('public/app/base.html');
});
app.get('/chapters', function(req, res) {
res.sendfile('public/app/base.html');
});
app.get('/api/*', function(req, res) {
generic_handler(req, res, 'api/'+req.params.join('/'));
});
function sanitize(req, res, next) {
for (var key in req.params) {
if (req.params[key].indexOf(".") > -1) {
return res.json(400, "Bad request");
}
}
next();
}
function generic_handler(req, res, dirpath) {
function callback(err, files) {
if (err) {
return notFound(err, res);
}
//
// Validate the files are json before we try and load them
//
var root = path.join(__dirname, dirpath);
var data = files.filter(function (file) {
return /.json$/.test(file);
}).map(function (file) {
var d;
try { d = require(path.join(root, file)); }
catch (ex) { d = null; }
return d;
}).filter(Boolean);
var submodules = files.filter(function (file) {
return fs.statSync(path.join(root, file)).isDirectory(); // TODO make async
});
var _files = files.filter(function (file) {
return fs.statSync(path.join(root, file)).isFile(); // TODO make async
});
res.json(200, {'module':path.basename(root), path:path.relative(__dirname, path.dirname(root)), data: data, submodules:submodules, files:_files});
}
fs.readdir(dirpath, callback);
}
function notFound(err, res) {
//
// TODO: send better error responses
//
res.writeHead(500, { 'content-type': 'application/json' });
return res.end(JSON.stringify({ error: err.message, reason: 'invalid parameters'}));
}
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});