-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.js
91 lines (83 loc) · 2.95 KB
/
handler.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
const fs = require('fs');
const audio = require('./audio.json');
const audioFiles = fs.readdirSync('./audio');
module.exports.handleRequest = function(req, res) {
var headers = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET',
'Access-Control-Allow-Headers': ['Content-Type', 'Content-Length'],
'Cache-Control': ['public', 'max-age=604800', 'immutable']
};
try {
//Create url to parse the path+query
//?format=mp3,wav,ogg,...
var filePath = "";
var url = req.url;
var format = "mp3";
if(req.url.indexOf('?') != -1) {
url = url.substring(0, req.url.indexOf('?'));
format = req.url.substring(req.url.indexOf('?') + 8);
}
console.log(url + "\n" + format);
if (req.method == 'GET' || req.method == 'HEAD') {
if(url == '/' || url == '/random') {
//Send back a random audio.
filePath = './audio/' + randomAudio();
//Update headers
headers['Cache-Control'] = ['max-age=0', 'must-revalidate'];
}
else if (url.indexOf('/audio') == 0) {
if(audio.includes(url.substring(7))) {
filePath = '.' + url;
}
else {
res.writeHead(404);
res.end('Audio File Not Found\n');
}
}
else if (url == '/test') {
res.writeHead(200);
res.end('Hello World\n');
}
else {
res.writeHead(404);
res.end('Not Found\n');
}
if (filePath != "") {
if(format == "mp3") {
var ctype = 'audio/mpeg';
filePath += '.mp3';
}
else if(format == "wav") {
ctype = 'audio/wav';
filePath += '.wav';
}
else if (format == "ogg") {
ctype = 'audio/ogg';
filePath += '.ogg';
}
console.log(filePath); //log filepath
var stat = fs.statSync(filePath);
res.writeHead(200, {
...headers,
'Content-Type': ctype,
'Content-Length': stat.size
});
fs.createReadStream(filePath).pipe(res);
}
} else {
res.writeHead(405);
res.end('Method not allowed. Use GET or HEAD.\n');
}
} catch (error) {
console.log(error);
res.writeHead(500);
res.end('An unexpected error has occurred.\n' + error);
}
return res;
}
//return random audio from the list (only multiples of 3 so only mp3 files)
function randomAudio() {
var rand = Math.floor(Math.random() * audio.length);
return audio[rand];
}