-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathroutes.js
57 lines (40 loc) · 1.45 KB
/
routes.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
// var mongoose = require('mongoose');
var mongojs = require('mongojs');
var express = require('express');
var fs = require('fs');
module.exports.routes = function(server, config) {
var db = mongojs(config.db);
var collection = db.collection('songs');
server.use(express.bodyParser());
//put the whole object in the db. This will contain a title, artist, and best path obj. (computed fingering data);
server.post('/upload', function(req,res) {
res.writeHead(200);
collection.insert(req.body);
res.end();
});
//actually sends back the midi files we have stored on the server.
server.get('/files/:song', function(req,res) {
res.writeHead(200);
fs.readFile('./TestMidiFiles/' + req.params.song, function(err, data) {
res.end(data);
});
});
//send back all best path objs we have in one request, so we don't need to make multiple requests as we get new songs.
server.get('/getAllPaths', function(req,res) {
collection.find(function(err, docs) {
if (err) {
res.json([]);
} else {
res.json(docs);
}
});
});
//hand back requested song name, so the XHTTP object on client can use that to request the real file.
//We don't just send back the file straight up because the XHTTP has special functions it uses
server.get('/songs/:song', function(req,res) {
res.writeHead(200, {
'Content-Type' : 'text/plain'
});
res.end(req.params.song + '.mid');
});
};