/
logic.js
49 lines (39 loc) · 1.42 KB
/
logic.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
const fs = require('fs');
const io = require('socket.io-client');
let dataFileObject = JSON.parse(fs.readFileSync('./data.txt', 'utf8')); //reads synchronously from data.txt file, and parses JSON to object
const socket = io('http://localhost:3000/'); //the location/port of our socket
// console.log(dataFileObject);
const update = (request) => {
updateJsonFile(request);
}
const search = (reqBody) => {
return getValue(reqBody);
}
const updateDataObject = (newObject) => { //updates dataFileObject to have KVPs from newObject
const keyValPairs = Object.entries(newObject);
keyValPairs.forEach(element => {
dataFileObject[element[0]] = element[1];
});
}
const updateJsonFile = (newJson) => {
updateDataObject(newJson);
fs.writeFile("./data.txt", JSON.stringify(dataFileObject), (err) => { //write stringified JSON of updated object to data.txt file
if (err) { //callback function for error
return console.log(err);
}
});
const msg = 'JSON file updated! new contents: ' + JSON.stringify(newJson); //string showing the changes made
socket.emit('update', msg);
}
const getValue = (key) => {
if (dataFileObject.hasOwnProperty(key)) {
return dataFileObject[key];
}
else {
return('error, key "' + key + '" not found in file');
}
}
module.exports = {
update: update,
search: search
}