title: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client in Express.js
body: <p>I read json file to query with req.params and I want to return res.json but I get error from express.js</p>
  
  <pre><code>_http_outgoing.js:482
      throw new ERR_HTTP_HEADERS_SENT('set');
      ^
  
  Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
  </code></pre>
  
  <p>in route.js</p>
  
  <pre><code>module.exports = (app) =&gt; {
    app.get('/find/:province', province.find);
  }
  </code></pre>
  
  <p>in controller.js</p>
  
  <pre><code>exports.find = function (req, res) {
    fs.readFile('./app/models/province_file.json', function(err, provinces){
      if (err) throw err;
      let obj_provinces = JSON.parse(provinces);
      let province = req.params.province;
      for (var i = 0; i &lt; obj_provinces.features.length; i++) {
        if (obj_provinces.features[i].properties.tb_tn == province) {
          let obj_result = obj_provinces.features[i].properties;
          console.log(obj_result);
          res.json(obj_result);
        }
      }
    });
  }
  </code></pre>
  
  <p>when I run the code I get error</p>
  
  <pre><code>_http_outgoing.js:482
      throw new ERR_HTTP_HEADERS_SENT('set');
      ^
  
  Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
      at ServerResponse.setHeader (_http_outgoing.js:482:11)
      at /Users/workspace/app/controllers/controller.js:87:13
      at FSReqCallback.readFileAfterClose [as oncomplete] (internal/fs/read_file_context.js:54:3)
  </code></pre>