-
Notifications
You must be signed in to change notification settings - Fork 3
/
bootstrap.js
185 lines (121 loc) · 6.45 KB
/
bootstrap.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
var http = require('http');
var fs = require('fs');
var url = require('url');
var querystring = require('querystring');
var waveformData = require('./library/waveformData');
var marked = require('marked');
var serverPort = process.env.PORT || 35000;
var serverIp = process.env.HOSTNAME || '127.0.0.1';
/**
*
* create a new nodejs server handle incoming requests
*
* @param {type} request
* @param {type} response
*/
var server = http.createServer(function (request, response) {
// parse the url
var urlParts = url.parse(request.url);
// check if its is the url of a javascript file
if (urlParts.pathname.split('.').pop() === 'js') {
// if the file exists send it to the client
// not really secure but this is a prototype
// TODO: filter the file request
fs.readFile('client' + urlParts.pathname, function (error, fileContent) {
if (!error) {
// send the static file to the client
response.writeHead(200, { 'Content-Type': 'application/javascript' });
response.write(fileContent);
response.end();
} else {
// the file was not on the server send a 404 page to the client
response.writeHead(404, { 'Content-Type': 'text/html' });
response.write('page not found');
response.end();
}
});
} else {
// handle the "routes"
switch (urlParts.pathname) {
case '/':
/*fs.readFile('client/index.html', function (error, html) {
if (!error) {
// send the main html page to the client
response.writeHead(200, { 'Content-Type': 'text/html' });
response.write(html);
response.end();
} else {
// the main page could not be found return a page not
// found message
response.writeHead(404, { 'Content-Type': 'text/html' });
response.write('page not found');
response.end();
}
});*/
fs.readFile('README.md', 'utf-8', function (error, document) {
if (!error) {
// send the main html page to the client
response.writeHead(200, { 'Content-Type': 'text/html' });
response.write(marked(document));
response.end();
} else {
// the main page could not be found return a page not
// found message
response.writeHead(404, { 'Content-Type': 'text/html' });
response.write('page not found');
response.end();
}
});
break;
case '/getwavedata':
var queryObject = querystring.parse(urlParts.query);
if (typeof queryObject !== 'undefined') {
waveformData.getRemoteWaveData(queryObject, function (error, peaks) {
if (!error) {
// success, send the track peaks to the client
response.writeHead(200, { 'Content-Type': 'application/json' });
response.write('{ "peaks": ' + JSON.stringify(peaks) + ' }');
response.end();
} else {
// fail, send the error to the client
response.writeHead(500, { 'Content-Type': 'application/json' });
response.write('{ error: ' + error + ' }');
response.end();
}
});
}
break;
case '/getTrack':
var queryObject = querystring.parse(urlParts.query);
//console.log(queryObject);
if (typeof queryObject !== 'undefined' && queryObject.trackId !== 'undefined' && queryObject.trackFormat !== 'undefined') {
var trackName = queryObject.trackId + '.' + queryObject.trackFormat;
if (queryObject.serverDirectory === undefined) {
queryObject.serverDirectory = './downloads';
}
var trackPath = queryObject.serverDirectory + '/' + trackName;
var fileStat = fs.statSync(trackPath);
var mimeType;
switch (queryObject.trackFormat) {
case 'ogg':
mimeType = 'audio/ogg';
break;
case 'mp3':
mimeType = 'audio/mpeg';
break;
}
response.writeHead(200, { 'Content-Type': mimeType, 'Content-Length': fileStat.size });
var readStream = fs.createReadStream(trackPath);
readStream.pipe(response);
}
break;
default:
response.writeHead(404, { 'Content-Type': 'text/html' });
response.write('page not found');
response.end();
}
}
});
server.listen(serverPort, serverIp, function () {
console.log('server is listening, ip: ' + serverIp + ', port: ' + serverPort);
});