-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
64 lines (60 loc) · 1.87 KB
/
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
import http from 'node:http';
import url from 'node:url';
import fs from 'node:fs';
// Get the helix IP from the environment variables.
const helixIp = process.env.HELIXIP;
const apiFunctions = [
'/getStatus',
'/getFiles',
'/playFile',
'/Event',
'/getTracks',
'/playTrack',
'/setVolume',
'/pickZone',
'/getGenericSettings',
];
const server = http.createServer((request, response) => {
// Deconstruct the request url intp parts.
let urlParts = url.parse(request.url);
if (apiFunctions.includes(urlParts.pathname)) {
// Proxy the API call to the Helix.
let requestUrl = 'http://' + helixIp + request.url;
http.get(requestUrl, res => {
let data = [];
res.on('data', chunk => {
data.push(chunk);
});
res.on('end', () => {
response.statusCode = 200;
response.setHeader('Content-Type', 'application/json');
response.end(Buffer.concat(data).toString());
});
});
} else if (request.url === '/favicon.ico') {
// Return the favicon.ico file.
try {
const data = fs.readFileSync('favicon.ico');
response.statusCode = 200;
response.setHeader('Content-Type', 'image/x-icon');
response.end(data);
} catch (err) {
console.error(err);
}
} else {
// Return the index.html page.
try {
const data = fs.readFileSync('index.html', 'utf8');
response.statusCode = 200;
response.setHeader('Content-Type', 'text/html');
response.end(data);
} catch (err) {
console.error(err);
}
}
});
const hostname = '127.0.0.1';
const port = 8000;
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});