-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
39 lines (31 loc) · 1.2 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
import verCompare from 'node-version-compare';
import express from 'express';
import versions from './data/version';
const app = express();
app.use('/firmware', express.static('firmware'));
app.get('/hello', (req, res) => res.send('World!'));
app.get('/', (req, res) => {
const [reqMac, reqDevice, reqVersion] = [
'X-ESP8266-MAC',
'X-ESP8266-DEVICE',
'X-ESP8266-VERSION',
].map(v => req.get(v.toLowerCase()));
console.log('[req]', new Date(), reqMac, reqDevice, reqVersion);
if (reqMac && reqDevice && reqVersion) {
const [resp = {}] = versions.filter(v => {
const { mac, device, version, min, max } = v.origin;
if (device !== reqDevice) return false;
console.log({ mac, reqMac });
if (mac !== '*' && mac !== reqMac) return false;
const minVersionCompare = verCompare(min, reqVersion);
const maxVersionCompare = verCompare(reqVersion, max);
if (min !== '*' && minVersionCompare === 1) return false;
if (max !== '*' && maxVersionCompare > 0) return false;
return true;
});
const { target = {} } = resp;
return res.json(target);
}
res.json({});
});
app.listen(3000, () => console.log('Update server listening on port 3000!'));