title: NodeJS HTTP response max size reached?
body: <p>I stumbled on a very strange issue recently concerning my Node.JS REST API. It's aim is to convert HTTP requests to SQL requests and the SQL response to HTTP response so I can then transfer everything over SSL/TLS and it's encoded.</p>
  
  <p>The problem is that when the response size is small, it all works fine but when it exceed a certain size (about 255.37 KB), the response body is cutted right in the middle of the JSON. After multiple tests it appears that the issue is related to the HTTP response total size (including the headers) because when some custom headers are removed, more of the body is sent. I wrote a similar code in PHP and the response in JSON from the PHP API is fine so I assumed that the issue was originating from a bug in the Node.JS web server. Also, the Content-Lenght header is fine and has the right value (like the PHP API response does).</p>
  
  <p>I'm using Node.JS v6.11.0 with the last release of Express. All my dependencies are up to date thanks to npm.</p>
  
  <p>Here is the code of the function that handle the GET HTTP requests and proceed in doing SELECT SQL requests, then parse to json string the answer
  <div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>function SelectData(connection){
   return function (req, res) {
      let tableName   = req.params.table;
      let id        = req.params.id;
  
      console.log("GET request received with tableName = " + tableName + " and id = " + id);
  
      // Should match this regex
      if (!tableName.match(/^[\-\_\s0-9A-Za-z]+$/)){
        console.log("Error: Invalid table name provided : " + tableName);
       badRequestError(req, res);
       return;
      }
      if (id !== undefined){
        if (!id.match(/^[0-9]+$/)){
          console.log("Error: Invalid id provided : " + id + " (table name was valid : " + tableName + ")");
          badRequestError(req, res);
          return;
        }
      }
      // if id is empty, then don't use any condition, else, add SQL condition
      idPart = (id == undefined) ? "" : ('WHERE id="' + id + '"');
      // TODO: try to replace " + var + " by question marks and bind params, find a way to do it w/ id
      connection.query("SELECT * FROM " + tableName + " " + idPart + ";", function(err, result){
        res.setHeader('Content-Type', 'application/json');
        console.log("Request executed successfully !");
        // Works too but I prefer the .send() method
        // res.status(200).write(JSON.stringify(result)).end();
        res.status(200).send(JSON.stringify(result));
        req.connection.destroy();
        return;
      });
    }
  }
  
  function badRequestError(req, res){
    res.setHeader('Content-Type', 'text/plain');
    res.status(400).send("Bad Request");
    req.connection.destroy();
  }
  
  exports.SelectData = SelectData;</code></pre>
  </div>
  </div>
  </p>
  
  <p>Edit: After researching yesterday I found similar issue resulting of the use of the HTTPS module so I removed it but it still happens.</p>