-
Notifications
You must be signed in to change notification settings - Fork 28
/
index.js
72 lines (62 loc) · 1.91 KB
/
index.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
var proxy = require('express-http-proxy');
var express = require('express');
var exphbs = require('express-handlebars');
var serveStatic = require('serve-static');
var glob = require('glob');
var path = require('path');
//get port from command line arguments
var port = process.argv[2] || 8001;
var app = express();
var NUPIC_SERVER = "http://localhost:8080";
var GIF_PATH = "static/data/gifData/";
//use handlebars render engine using the existing locations
app.engine('.html', exphbs({
extname: '.html',
defaultLayout: 'layout',
layoutsDir: "tmpl/"
}));
app.set('view engine', '.html');
app.set('views', 'html/');
//paths
app.use('/static', serveStatic(__dirname + '/static'));
app.use('/client/ep11/audio/casio', serveStatic(__dirname + '/static/js/ep11/audio/casio'));
app.get('/', index);
app.get('/client/:ep/:file', client);
app.use('/_proxy', proxy(NUPIC_SERVER));
app.get('/_giflist', gifList);
//serve
app.listen(port);
console.log('htm-school-viz running on http://localhost:' + port);
//handlers
function index(req, res) {
res.render('index', {
name: 'index',
title: 'HTM School Visualizations'
});
}
function client(req, res) {
var name = (req.params.ep + '/' + req.params.file).split('.')[0];
res.render(name, {
name: name,
title: title(name)
});
}
function gifList(req, res) {
glob(path.resolve(__dirname, GIF_PATH, '*.json'), function(err, list) {
res.json({
gifs: list.map(function(abspath) {
return {
path: '/' + path.relative(__dirname, abspath),
dimensions: require(abspath).dimensions
};
})
});
});
}
//helpers
//Replaces dashes by space and capitalizes every word
function title(str) {
return str.replace(/-/g, ' ').replace(/\w+/g, function(txt) {
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
});
}