-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecorder_server.js
75 lines (61 loc) · 2.14 KB
/
recorder_server.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
const fs = require('fs');
const http = require('http');
const qs = require('querystring');
const static = require('node-static');
const cookie = require('cookie');
const recorderDocument = fs.readFileSync('recorder.html', 'utf-8');
const playerDocument = fs.readFileSync('player.html', 'utf-8');
const fileServer = new static.Server();
const requestListener = function (request, response) {
// Serve static files.
if (request.method === 'GET' && request.url.startsWith('/assets/')){
fileServer.serve(request, response);
}
else if (request.method === 'GET' && request.url.startsWith('/json/')) {
fileServer.serve(request, response);
}
// Serve dynamic recorder-page.
else if (request.method === 'GET' && request.url === '/') {
// Following retrieves file names in json-directory.
fs.readdir('json', function (error, fileNames) {
if (error) {
response.writeHead(500);
response.end(error);
} else {
let splitDoc = recorderDocument.split("<option>-!-</option>");
let result = "";
for (let i = 0; i < fileNames.length; i++) {
let string = "<option>"+ fileNames[i] + "</option>";
result += string;
}
let lengthOfLast = splitDoc.length;
let newDoc = splitDoc[0] + result + splitDoc[lengthOfLast-1];
response.writeHead(200);
response.end(newDoc);
}
});
}
// Handle POST-requests.
else if (request.method === 'POST') {
let body = "";
request.on('data', function (chunk) {
body += chunk;
});
request.on('end', function () {
let query = qs.parse(body);
let userChose = qs.parse(body);
let notesSelect = userChose.note_names;
let chosenNotes = cookie.serialize('notes', notesSelect);
response.setHeader('Set-Cookie', chosenNotes);
const documentParts = playerDocument.split('-!-');
response.writeHead(200);
response.end(documentParts[0] + query.note_names + documentParts[1]);
});
}
else {
response.writeHead(404);
response.end('404 Not found.');
}
};
const server = http.createServer(requestListener);
server.listen(3000);