Company logo
  • Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Plans
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Calculator

0

109
Views
NodeJS - Removing a variable from a text file (Replacing with nothing then removing the blank line)

Using the below code, I am storing one-time use tokens in a text file, upon being submitted in a POST, the script checks the file to see if it exists and then I need it to remove it. Currently, I got as far as replacing it with a blank line as seen below. How do I remove the blank line afterwards, or just remove the token with the line in the first place? I have used the shift feature before however, this wouldn't apply as the token can be on any line in the text document

var data = fs.readFileSync('tokens.txt', 'utf-8');
if(data.includes(req.body.token)){
    var newValue = data.replace(new RegExp(req.body.token), '');
    fs.writeFileSync('tokens.txt', newValue, 'utf-8');
8 months ago ยท Santiago Trujillo
Answer question
Find remote jobs