title: Retrieving JSON object from a multipart request in nodejs
body: <pre><code>var form = new formidable.IncomingForm();
  form.parse(req, function(err, fields, files) {
      if (err) {
          res.status(200).send('parsing error');
      }
  });
  
  var jsonObject;
  var imageArray = [];
  
  form.on('file', function(field, file) {
      console.log(file);
      if (file.type == 'application/json') {
          jsonObject = JSON.parse(fs.readFileSync(file.path, 'utf8'));
      } else if (file.type == 'image/jpeg') {
          imageArray.push(file.path);
      }
  })
  </code></pre>
  
  <p>I'm using <a href="https://github.com/felixge/node-formidable" rel="nofollow">formidable</a> to handle a multipart request in node.js made from my iOS app, the request usually contains "n" number of images and a associated JSON object, the formidable middleware treats the JSON as an file and stores the file in the temporary download location and provides a path to the file, the code "jsonObject = JSON.parse(fs.readFileSync(file.path, 'utf8'));" reads the file and parses into a JSON object. Is this how it is supposed to work or is there a better way, that negates the superfluous file write and read from the disk and provides a parsed JSON obj directly.</p>
  
  <p>PS - newbie to node.js, heck newbie to entire backend system with a very bad case OCD.</p>