title: Is there a better way to accept JSON data than appending chunked input to a string and running `JSON.parse()` upon it afterward?
body: <p><strong>Is there a better way, in Node.js, to accept JSON data than appending chunked input to a string and running <code>JSON.parse()</code> upon it afterward?</strong> The server can not assume that the JSON input is valid, and <code>JSON.parse()</code> is the only way I'm aware of to validate JSON data.</p>
  
  <p>Assume the following server accepts:</p>
  
  <ul>
  <li>only POST requests</li>
  <li>only JSON data</li>
  <li>only with a <code>Content-Type</code> header of <code>"application/json"</code></li>
  </ul>
  
  <p>.</p>
  
  <pre><code>var server = http.createServer(function(req, res) {
      ....
  
      var parsedData;
      var inputData = '';
  
      req.on('data', function(chunk) {
          inputData += chunk;
      });
  
      req.on('end', function() {
          parsedData = JSON.parse(inputData);
      }
  
      ....
  }
  </code></pre>
  
  <p>If I am enforcing the input data so strictly, it seems strange to simply append this data to a string and then <code>JSON.parse()</code> it, essentially (assuming proper JSON is inputted) going <code>JSON -&gt; string -&gt; JSON</code>. (Of course, one can not assume the data is valid.)</p>