-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
78 lines (61 loc) · 1.78 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
74
75
76
77
const express = require('express');
const path = require('path');
const fs = require('fs');
const papa = require("papaparse");
const app = express();
const port = 9000;
const password = 'password'
function saveToFile(filename, newData) {
fs.readFile(`${filename}.json`, 'utf8', (err, dataString) => {
if (err) {
console.log(`Could not find ${filename}. Creating an empty file.`);
var data = [];
} else {
console.log("Opened the file")
var data = JSON.parse(dataString);
}
data.push(newData);
write(`${filename}.json`, JSON.stringify(data));
write(`${filename}.csv`, papa.unparse(data));
});
}
function write(filename, data) {
fs.writeFile(filename, data, function(err) {
if(err) {
console.log(err);
}
console.log(`Writing ${filename}`);
});
}
app.use(express.static(path.join(__dirname, '/')));
app.use(express.json());
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, '/index.html'));
})
app.post('/save/Sheet1', (req, res) => {
let data = req.body[0];
let fixed_data = {};
Object.keys(data).forEach(element => {
let [key, value] = data[element].split(': ');
fixed_data[key] = value;
});
if (fixed_data.sort != 'no_data') {
let statementRankings = fixed_data.sort.split('|');
statementRankings.forEach((element, i) => {
fixed_data[`s${i}`] = element;
});
}
saveToFile('data', fixed_data);
res.send({});
})
app.get('/download/:format', (req, res) => {
if (req.query.password && req.query.password == password) {
res.download(path.join(__dirname, `/data.${req.params.format}`));
}
else {
res.send('Password missing or incorrect.')
}
})
app.listen(port, () => {
console.log(`Starting the server on port ${port}`);
})