title: Node.js Server: Image Upload / Corruption Issues
body: <p>So I'm trying to write a basic file server in Node.js, and all the images I've tried uploading and storing on it are coming back as corrupted. The problem seems to have something to do with the way that Node Buffers handle being converted to UTF-8 and back again (which I have to do in order to get the POST body headers out and away from the binary data). </p>
  
  <p>Here's a simple Node server that shows my current approach and the problems I've been having:</p>
  
  <pre><code>var http = require('http');
  
  var server = http.createServer(function(request, response) {
      if (request.method === "GET") {
          // on GET request, output a simple web page with a file upload form
          var mypage = '&lt;!doctype html&gt;&lt;html&gt;&lt;head&gt;&lt;meta charset="utf-8"&gt;' + 
                          '&lt;title&gt;Submit POST Form&lt;/title&gt;&lt;/head&gt;\r\n&lt;body&gt;' +
                          '&lt;form action="http://127.0.0.1:8008" method="POST" ' + 
                          'enctype="multipart/form-data"&gt; &lt;input name="upload" ' + 
                          'type="file"&gt;&lt;p&gt;&lt;button type="submit"&gt;Submit&lt;/button&gt;' + 
                          '&lt;/p&gt;&lt;/form&gt;&lt;/body&gt;&lt;/html&gt;\r\n';
          response.writeHead(200, {
              "Content-Type": "text/html",
              "Content-Length": mypage.length
          });
          response.end(mypage);
  
      } else if (request.method === "POST") {
          // if we have a return post request, let's capture it
          var upload = new Buffer([]);
  
          // get the data
          request.on('data', function(chunk) {
              // copy post data 
              upload = Buffer.concat([upload, chunk]);
          });
  
          // when we have all the data
          request.on('end', function() {
              // convert to UTF8 so we can pull out the post headers
              var str = upload.toString('utf8');
              // get post headers with a regular expression
              var re = /(\S+)\r\nContent-Disposition:\s*form-data;\s*name="\w+";\s*filename="[^"]*"\r\nContent-Type: (\S+)\r\n\r\n/i,
                  reMatch = str.match(re);
              var lengthOfHeaders = reMatch[0].length,
                  boundary = reMatch[1],
                  mimeType = reMatch[2];
              // slice headers off top of post body
              str = str.slice(lengthOfHeaders);
              // remove the end boundary
              str = str.replace("\r\n" + boundary + "--\r\n", '');
              // convert back to buffer
              var rawdata = new Buffer(str, 'utf8');
              // echo back to client
              response.writeHead(200, {
                  "Content-Type": mimeType
              });
              response.end(rawdata);
          });
      }
  });
  
  server.listen(8008);
  console.log("server running on port 8008");
  </code></pre>
  
  <p>To test it, run the script in node and go to 127.0.0.1:8008 in your browser. Try uploading an image and submitting the form. The image comes back as corrupt every time -- even though the script should just be directly echoing the image data back to the browser.</p>
  
  <p>So does anyone know what I'm doing wrong here? Is there a better way to handle POST body headers in Node that I haven't figured out yet? (And before anyone says anything, no, I <em>don't</em> want to use Express. I want to figure out and understand this problem.)</p>