-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
40 lines (37 loc) · 1.24 KB
/
index.js
File metadata and controls
40 lines (37 loc) · 1.24 KB
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
var fs = require('fs');
module.exports = async function (context, req) {
var entries = fs.existsSync('./last.json') ? JSON.parse(fs.readFileSync('./last.json')) : [];
if (Array.isArray(entries)) {
var cutoff = (new Date()).getTime() - (5 * 60 * 1000); // five minutes old entries get deleted
entries = entries.filter(rec => rec.ts > cutoff);
} else {
entries = [];
}
if (req.body.isGet) {
if (req.body.atCar) {
var which = req.body.atCar.toLowerCase();
var entry = entries.find(rec => rec.atCar == which);
} else {
var entry = {};
}
context.res = {
status: 200,
headers: {
'Content-Type': 'application/json'
},
body: entry ? entry : {}
};
} else {
var entry = req.body;
entry.atCar = entry.forCar.toLowerCase();
entry = { ts: (new Date()).getTime(), ...entry };
entries = entries.filter(rec => rec.atCar != entry.atCar);
entries.push(entry);
fs.writeFile('./last.json', JSON.stringify(entries), function (err) {
context.res = {
status: 400,
body: err
};
});
}
};