-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
65 lines (47 loc) · 1.33 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
var express = require('express'),
app = express();
var path = require('path');
var twig = require('twig');
twig.cache(false);
app.engine('twig', twig.renderFile);
app.use('/scripts', express.static(path.join(__dirname, '/scripts')));
app.use('/stylesheets', express.static(path.join(__dirname, '/stylesheets')));
app.use('/vendor', express.static(path.join(__dirname, '/vendor')));
var router = express.Router();
router.get('/', function (req, res) {
res.render('chord.twig');
});
router.get('/files/:artist/:song.:ext', function (req, res, next) {
var artist = req.params.artist,
song = req.params.song,
extension = req.params.ext;
var directory;
switch (extension) {
case 'pdf':
directory = '/path/to/pdf/folder';
break;
case 'chopro':
case 'pro':
directory = '/path/to/chordpro/folder';
break;
default:
next();
return;
}
var fs = require("fs");
var filepath = directory + '/' + artist + '/' + song + '.' + extension;
if (!fs.existsSync(filepath)) {
res.status(404).send('Not found.');
return;
}
fs.readFile(filepath, function (err, data) {
if (err) throw err;
if (extension !== 'pdf') {
// Response will be text based.
data = data.toString();
}
res.send(data);
});
});
app.use('/library', router);
app.listen(8080);