title: How to call fs.readFile if the file has changed
body: <p>I have a json data that contain some information, the contents of data are derived from post method route in express js. If post method get a new data, fs.writeFile delete all data content and write a new data.</p>
  
  <p>I want call fs.readFile to read the new data automatically after fs.writeFile finish write a new data.</p>
  
  <pre><code>const express = require('express');
  const bodyParser = require('body-parser');
  const fs = require('fs');
  const path = require('path');
  const app = express();
  
  const DATA_FILE = path.join(__dirname, 'data.json');
  
  app.use(bodyParser.json());
  
  app.post('/data', (req, res) =&gt; {
      // delete all content on data.json file
      fs.writeFile(DATA_FILE, '', (err) =&gt; {
          if (err) throw err;
      });
      const newData = req.body.data;
      // write new data to data.json file
      fs.writeFile(DATA_FILE, JSON.stringify(newData, null, 4), () =&gt; {
          res.json(newData);
          console.log('New data added');
      });
  });
  
  // read new data from data.json file
  fs.readFile(DATA_FILE, (err, data) =&gt; {
      if (err) throw err;
      const readNewData = JSON.parse(data);
      console.log(readNewData);
  });
  
  module.exports = app;
  </code></pre>
  
  <p>fs.readFile located outside of post method because i want to run fs.readFile automatically if the express js start, does not have to wait for action from the post method.</p>
  
  <p>How to call fs.readFile automatically after fs.writeFile has finished write a new data?</p>