-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
68 lines (55 loc) · 1.89 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
65
66
67
68
const express = require('express');
const childProcess = require('child_process');
const path = require('path');
const { Keys, KeyBinaryMapping } = require('./src/data/RemoteKeys');
const config = require('./config');
const app = express();
app.use(express.static(__dirname));
// Express route for incoming requests for a customer name
app.get('/api/tv/*', (req, res) => {
const { url } = req;
if (url.includes('KEY_')) {
const key = url.substring(8);
if (Keys.includes(key)) {
const command = `sudo ${config.pathToExecutable} ${KeyBinaryMapping[key]}`;
childProcess.exec(command);
res.status(200).send(key);
} else {
res.status(404).send(`Unrecognised API call - unknown key: ${key}`);
}
} else {
res.status(404).send('Unrecognised API call');
}
});
app.get('/', (req, res) => {
res.status(200).sendFile(path.join(__dirname, 'dist', 'remote.html'));
});
app.get('/js/*', (req, res) => {
res.status(200).sendFile(path.join(__dirname, 'dist', req.originalUrl));
});
app.get('/css/*', (req, res) => {
res.status(200).sendFile(path.join(__dirname, 'dist', req.originalUrl));
});
app.get('/images/*', (req, res) => {
res.status(200).sendFile(path.join(__dirname, 'dist', req.originalUrl));
});
app.get('/data/*', (req, res) => {
res.status(200).sendFile(path.join(__dirname, 'dist', req.originalUrl));
});
// Express route for any other unrecognised incoming requests
app.get('/api/*', (req, res) => {
res.status(404).send('Unrecognised API call');
});
app.get('*', (req, res) => {
res.status(404).send('Unrecognised path');
});
// Express route to handle errors
app.use((err, req, res, next) => {
if (req.xhr) {
res.status(500).send('Oops, Something went wrong!');
} else {
next(err);
}
});
app.listen(config.port);
console.log(`Server running at port ${config.port}`);