title: How to handle form data in an http requests in nodejs
body: <p>I am writing a code to let the client upload two files on the server. Since I used the director router, I have set a listener like this:</p>
  
  <pre><code>request.chunks = [];
  request.on('data', function (chunk) {
      request.chunks.push( chunk.toString());
  };
  </code></pre>
  
  <p>and here is the console.log of the chunks when a client uploads a file (based on the browser the boundaries change):</p>
  
  <pre><code>-----------------------------7dd2c419180232
  Content-Disposition: form-data; name="filename"
  
  
  -----------------------------7dd2c419180232
  Content-Disposition: form-data; name="uploadfile"; filename="first.txt"
  Content-Type: application/octet-stream
  
  the content of first file
  
  -----------------------------7dd2c419180232
  Content-Disposition: form-data; name="wfilename"
  
  
  -----------------------------7dd2c419180232
  Content-Disposition: form-data; name="wuploadfile"; filename="second.txt"
  Content-Type: application/octet-stream
  
  the content of the second file
  
  -----------------------------7dd2c419180232--
  </code></pre>
  
  <p>I have handled the problem by a few regular expressions for extracting each file-name and each file-content on the <code>request.chunks</code> variable, but the browsers have different tendencies (for these boundries, for example for google chrome is like this: '------WebKit...') and I wonder if there is a direct way to parse file-name and file-content (obviously from <code>request.chunks</code> not <code>request</code>) with some modules like formidable or multi-part or querystring?</p>
  
  <hr>
  
  <p>Thanks to @micnic, I came up with a parser for the headers.  It may need revisions which are welcome at this level:</p>
  
  <pre><code>exports.parseMultipart = function(request) {
  
      // Convert the chunks to string
      var str = request.chunks.toString();
  
      // Get the boundry out pf header
      var boundry = '--' + request.headers["content-type"].substring(request.headers["content-type"].indexOf('=')+1, request.headers["content-type"].length);
  
      // Initialization
      var request_data = {};
      index = 0;
  
  
      // For each form element, store the value in request_data
      while (str.indexOf(boundry, index) != -1) {
          index += boundry.length;
          i = str.indexOf(" name=\"",index);
          j = str.indexOf("\"",i+7);
          name = str.substring(i+7,j);
          var value = {};
          if (str.charAt(j+1)==';') {
              value["type"] = "file";
              i = j + 3;
              j = str.indexOf("\"",i+14);
              filename = str.substring(i+10, j);
              value["filename"] = filename;
              i = j + 17;
              j = str.indexOf("\r", i);
              contentType = str.substring(i, j);
              value["content-type"] = contentType;
              i = j + 4;
              j = str.indexOf("\n\r\n" + boundry, i);
              fileContent = str.substring(i, j);
              value["content"] = fileContent;
          } else {
              value["type"] = "field";
              i = j + 5;
              j = str.indexOf("\r\n" + boundry,i);
              value["content"] = str.substring(i,j);
          }
          index = str.indexOf(boundry, index) + 2;
          request_data[name] = value;
      }
      return request_data;
  }
  </code></pre>