-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
67 lines (53 loc) · 1.77 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
const express = require('express');
const cors = require('cors');
const fileUpload = require('express-fileupload');
const fs = require('fs');
const app = express();
app.use(cors())
app.use(fileUpload());
app.get('/download', (req, res) => {
return res.download("./app-log.json");
})
app.post('/upload', (req,res) => {
if(req.files === null) {
return res.status(400).json({msg: 'No file uploaded' });
}
const file = req.files.file;
const data = Buffer.from(file.data).toString();
console.log(data);
const parsed = [];
const lines = data.split("\n");
for(let i = 0 ; i< lines.length; i++) {
const line = lines[i];
if(line == "") {
continue;
}
const content = line.split(" - ");
console.log("content", content);
const datetime = content[0];
const loglevel = content[1];
const transaction = JSON.parse(content[2]);
console.log(transaction.transactionId);
parsed.push({
timestamp: new Date(datetime).getTime(),
loglevel,
transactionId: transaction.transactionId,
err: transaction.err,
});
}
fs.writeFile("./app-log.json", JSON.stringify(parsed), (err) => {
const readstream = fs.createReadStream("./app-log.json");
readstream.pipe(res);
});
// return res.json(parsed);
// console.log(file);
// return res.json("ok");
// file.mv(`${__dirname}/client/public/uploads/${file.name}`, err => {
// if(err){
// console.error(err);
// return res.status(500).send(err);
// }
// res.json({ fileName: file.name, filePath:`/uploads/${file.name}`})
// });
});
app.listen(5000,() => console.log('Server Started...'));