We have an upcoming project at work which is going to require working with express.js. I have no prior experience with node.js, so I thought I'd try and do something aside from some courses. Are there any anti-patterns and what can be improved?
Objective
I have a text file that is retrieved from an external source and looks like this (It has a lot more lines than this)
I do not need the first 3 lines, but I do need the data separated by a "|". Wherever there is a "||" it adds an undefined value to the array so I just remove it. I also do not need any text which has a "/" in it.
2019-10-09 11:03:33 : ============================= ATTACK LOG ==============================
2019-10-09 11:03:33 : | ------- LOOT ------ ------ BONUS ------
2019-10-09 11:03:33 : |AC|TIME.|TROP.|SRC|DS| GOLD| ELIXIR| DE|TR.|S| %| GOLD|ELIXIR| DE|L.
2019-10-09 11:15:44 : 1|11:15| 1761| 17| 4| 450447| 546344|1704|-17|0| 47| 0| 0| 0|--||211/220|
2019-10-09 11:20:49 : 1|11:20| 1744| 14| 4| 307817| 279805| 643| 10|1| 62| 15640| 15640| 0|G1||209/220|
I am using a library called line-reader to read the text file and my Mongoose schema is defined as Results
router.get('/retrieve-logs', (req,res,next)=>{
var headers = ['accountNumber','timeOfAttack',
'trophies','searches','ds',
'gold','elixir','darkElixir',
'trophyChange','victory','damagePercentage',
'bonusGold','bonusElixir','bonusDarkElixir'];
const filePath = 'AttackLog-2019-10.log'
//Read from each line
lineReader.eachLine(filePath, (line)=> {
//Do nothing if 'ATTACK LOG' in line
if (line.includes('ATTACK LOG')) {
} else {
//Remove appended date and time
removedDateTime = line.substring(22);
if (removedDateTime.includes('GOLD')) {
} else {
//Grab lines after headers removed
attackData = removedDateTime.split("|");
tempAttackData = [];
//Remove undefined from array of data and remove last value from array
for(let i of attackData)
i && tempAttackData.push(i);
attackData = tempAttackData;
//Remove last item from array which we don't need
attackData.pop();
// Remove excess whitespace
cleanedAttackData= [];
attackData.forEach((data, index) => {
cleaned = data.trim();
cleanedAttackData.push(cleaned)
});
//Create object for DB {headers: attackData}
var result = {};
headers.forEach((headers, i) => result[headers] = cleanedAttackData[i]);
//Save the created object
finalResults = new Results(result)
finalResults.save();
}
}
})
Results.find({},(err, docs) => {
res.status(200).send({
message: "Results successfully loaded",
success: docs
})
})
})