-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
121 lines (101 loc) · 3.7 KB
/
app.js
File metadata and controls
121 lines (101 loc) · 3.7 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
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
const express = require('express');
const app = express();
const path = require('path');
const fs = require('fs');
const bodyParser = require('body-parser');
const csvParser = require('csv-parser');
const { networkInterfaces } = require('os');
const nets = networkInterfaces();
const results = Object.create(null);
var jsonParser = bodyParser.json();
function jsonToCsv(jsonData) {
const keys = Object.keys(jsonData[0]);
const csvRows = [];
// Add headers
csvRows.push(keys.join(';'));
// Add data rows
jsonData.forEach(item => {
const row = keys.map(key => item[key]).join(';');
csvRows.push(row);
});
return csvRows.join('\n');
}
function formatDate(date) {
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0'); // Months are zero-based
const day = String(date.getDate()).padStart(2, '0');
return `${year}_${month}_${day}`;
}
app.use(express.static(path.join(__dirname, 'public')))
.set('views', path.join(__dirname, 'views'))
.set('view engine', 'ejs');
app.get('/', (req, res) => {
// Get IP Address
for (const name of Object.keys(nets)) {
for (const net of nets[name]) {
// Skip over non-IPv4 and internal (i.e. 127.0.0.1) addresses
// 'IPv4' is in Node <= 17, from 18 it's a number 4 or 6
const familyV4Value = typeof net.family === 'string' ? 'IPv4' : 4
if (net.family === familyV4Value && !net.internal) {
if (!results[name]) {
results[name] = [];
}
results[name].push(net.address);
}
}
}
// Render
res.render('index', {
networkData: results
});
});
app.post('/csv', jsonParser, (req, res) => {
const data = req.body.data;
const device = req.body.device;
// Check if the received data is an array
// if (!Array.isArray(data)) {
// return res.status(400).json({
// message: 'Invalid data format, expected an array of objects'
// });
// }
const currentDate = new Date();
const formattedDate = formatDate(currentDate);
if (fs.existsSync(path.join('D:', 'exported_csv', formattedDate + '_' + device + '.csv'))) {
let csvRow = []; ``
fs.createReadStream(path.join('D:', 'exported_csv', formattedDate + '_' + device + '.csv')).pipe(csvParser({ separator: ';' })).on('error', err => { // Read Current File
console.log(err);
}).on('data', data => { // Current CSV to JSON
csvRow.push(data);
}).on('end', end => { // Current Data + New Data
for (var i = 0; i < data.length; i++) {
csvRow.push(data[i]);
}
var appendData = jsonToCsv(csvRow);
// Write CSV
fs.writeFile(path.join('D:', 'exported_csv', formattedDate + '_' + device + '.csv'), appendData, (err) => {
if (err) {
return res.status(500).json({
message: 'Failed to write CSV file'
});
}
res.status(200).json({
message: 'Write CSV Success'
});
});
});
} else {
var appendData = jsonToCsv(req.body.data);
// Write CSV
fs.writeFile(path.join('D:', 'exported_csv', formattedDate + '_' + device + '.csv'), appendData, (err) => {
if (err) {
return res.status(500).json({
message: 'Failed to write CSV file'
});
}
res.status(200).json({
message: 'Write CSV Success'
});
});
}
});
app.listen(process.env.port || 3000);