title: Read File then delete the file with node.js
body: <p>I am attempting to use nodeJS to do the following:</p>
  
  <p>Start an executable file (executable file then creates a timestamp_Question.txt file), read the created .txt file and return the text to my front end. Accept user input and create a new timestamp_Answer.txt file with a new name containing the user input, and then delete the original txt file.</p>
  
  <p>Everything seems to be working except the deletion of the original file which is getting the following error</p>
  
  <blockquote>
    <p>Error: EBUSY: resource busy or locked, unlink 'c:\projects\pizzabox\95912_Questi
    on.txt'
        at Error (native)</p>
  </blockquote>
  
  <p>Here are the various segments of my server code:</p>
  
  <p>Starting the .exe file:</p>
  
  <pre><code>  startProgram: function(req, res){
      var date = new Date().toLocaleTimeString();
      date = date.replace(/\D/g,'');
      exec('C:/projects/pizzabox/server/webserver/Pizza_page_server.exe', [date], function(err, data) {  
       console.log(err)
      });
      res.send(date)  /// front end needs date value to track file as date is attached to created txtfile
    }
  </code></pre>
  
  <p>Front end requesting the contents of the txt created by the exe:</p>
  
  <pre><code> getDocument: function(req,res){
      console.log('file requested', req.params.dateStamp);
      var directory = 'C:/projects/pizzabox/'
      var fileCode = req.params.dateStamp.toString()
      var fileSuffix = "_Question.txt"
      var file = directory+fileCode+fileSuffix
      console.log('file is', file);
  
  
      while(!fs.existsSync(file)){};
      fs.readFile(file, 'utf8', function(err,data){
        if (err) throw err;
        console.log('read ', data);
        res.send(data)
      });
  </code></pre>
  
  <p>Front end submitting data to create new timestamp_Answer.txt file and deletion of old timestamp_Question.txt file:</p>
  
  <pre><code>  sendValue: function(req,res){
      console.log('got value', req.body.value, " questionNumber ", req.body.questionNumber, "timestamp ", req.body.timestamp);
      fs.appendFile(req.body.timestamp+'_Answer.txt', "Question_Num:"+req.body.questionNumber+"\nAnswer_Val:"+req.body.value, function(err){
        if(err) throw err;
        console.log('file created!');
  
        fs.unlink(req.body.timestamp+'_Question.txt', function(err){
          if (err) throw err;
          console.log('question file deleted');
        })
      })
    }
  </code></pre>
  
  <p>Any reason why unlink wouldn't have access to the file? does fs.readFile() not close/exit the file after it completes? </p>