-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
73 lines (65 loc) · 1.7 KB
/
app.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
const express = require('express');
const cors = require('cors')
const util = require('util');
const fs = require('fs');
const bodyParser = require('body-parser');
const serveIndex = require('serve-index');
const ejs = require('ejs');
const parseLog = require('./parseLog');
const readdir = util.promisify(fs.readdir);
const mkdir = util.promisify(fs.mkdir);
const writeFile = util.promisify(fs.writeFile);
const PORT=3337
const getFileName = (session) => {
let date = new Date();
return `log/${session}-${date.toISOString().replace(/:|\./g, '-')}.txt`;
};
const app = express();
app.use(cors());
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
app.use(
"/log",
serveIndex("log", {
icons: true,
view: "details"
})
);
app.use(express.static('public'));
// health check url
app.get('/', function (req, res) {
res.send('OK');
});
app.post('/logger', function(req, res) {
let data = req.body.data;
if (!data || data.length === 0) {
res.send('NODATA');
return;
}
writeFile(getFileName(req.body.session), data.join('\n'))
.then(() => {
res.send('LOGGED');
})
.catch(() => {
res.send('ERR');
});
});
// custom display for log files
const displayLogsTemplate = fs.readFileSync("./display_logs.ejs", "utf8");
app.get("/log/:logFile", function(req, res) {
parseLog(req.params.logFile).then(data => {
res.send(ejs.render(displayLogsTemplate, data, {}));
});
});
readdir('log')
.catch((err) => {
return mkdir('log');
})
.then(() => {
// start app after log directory created
app.listen(PORT, function () {
console.log(`Remote logging app on port ${PORT} !`);
});
});