-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
142 lines (131 loc) · 3.91 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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
var express = require('express');
var app = express();
var fs = require('fs');
var bodyParser = require('body-parser');
var cors = require('cors');
var moment = require('moment');
var j2xls = require('json2xls-xml')({ pretty: true });
var address,
os = require('os'),
ifaces = os.networkInterfaces();
var portNum = 80;
var data = [];
var oldData = [];
var dir = './reports/';
var teams_dir = './teams/';
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
if (!fs.existsSync(teams_dir)) {
fs.mkrdirSync(teams_dir);
}
var fileName = dir + moment().format('M;D;YYYY;H;mm;ss');
app.use(bodyParser.json({ limit: '1000mb' }));
app.use(
bodyParser.urlencoded({
extended: true,
limit: '100mb'
})
);
var fileName = dir + moment().format("M;D;YYYY;H;mm;ss");
fs.readdirSync(dir).forEach((file) => {
// you may want to filter these by extension, etc. to make sure they are JSON files
if (file.includes(".json") && !file.includes(fileName)) {
try {
records = JSON.parse(fs.readFileSync(dir + file, 'utf8'))
} catch (e) { return; }
for (var i = records.length - 1; i >= 0; i--) oldData.push(records[i])
}
})
app.use(bodyParser.json({ limit: '1000mb' }));
app.use(bodyParser.urlencoded({
extended: true,
limit: '100mb'
}));
function newRecords(records) {
var team_data;
var team_num = records[0].team;
for (var i = records.length - 1; i >= 0; i--) {
convert(records[i])
data.push(records[i])
console.log(records[i])
}
fs.writeFile(fileName + ".xlsx", j2xls(data), 'binary', function(err) {
error(err);
});
fs.writeFile(fileName + ".json", JSON.stringify(data, null, 4), 'binary', function(err) {
error(err);
});
/*
The code below writes to the json file of the corresponding team
*/
if (!fs.existsSync(teams_dir + team_num + ".json")) {
var team_data = fs.readFileSync(teams_dir + 'template.json', 'utf-8');
team_data.team_number = team_num;
} else {
var team_data = fs.readFileSync(teams_dir + team_num + '.json', 'utf-8');
}
team_data = JSON.parse(team_data);
/*
The code below sets the variables in the json to info from the submission forms
*/
team_data.match_history
fs.writeFileSync(teams_dir + team_num + ".json", team_data, function(err) {
if (err) throw err;
})
}
app.use(cors());
app.use('/', express.static(__dirname + '/dist'));
fs.writeFile(fileName + '.xlsx', j2xls(data), 'binary', function(err) {
error(err);
});
fs.writeFile(fileName + '.json', JSON.stringify(data, null, 4), 'binary', function(err) {
error(err);
});
app.post('/api', function(req, res) {
newRecords(req.body)
res.sendStatus(200);
});
app.get('/api', function(req, res) {
allData = []
for (var i = oldData.length - 1; i >= 0; i--) {
allData.push(oldData[i])
}
for (var i = data.length - 1; i >= 0; i--) {
allData.push(data[i])
}
res.send(JSON.stringify(allData, null, 4));
});
app.get('/download', function(req, res) {
res.writeHead(200, {
'Content-Type': 'application/force-download',
'Content-disposition': 'attachment; filename=latest.xlsx'
});
res.end(j2xls(data));
});
app.get('/search', function(req, res) {
var query = req.body['SearchBar'];
});
serverStopper = app.listen(portNum, function() {});
for (var dev in ifaces) {
var iface = ifaces[dev].filter(function(details) {
return details.family === 'IPv4' && details.internal === false;
});
if (iface.length > 0) address = iface[0].address;
}
console.log(address);
function error(err) {
if (err) {
return console.log(err);
}
}
function convert(obj) { //turns object.arrays into strings
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
if (obj[key] instanceof Array) {
obj[key] = obj[key].toString()
}
}
}
return obj;
}