title: Handle AJAX image upload with node and express
body: <p>I would like to upload an image to my node server using AJAX;</p>
  
  <p>from the application I make the following:</p>
  
  <pre><code>var canvas = document.getElementById('canvas' + index);
  var dataURL = canvas.toDataURL();
  
  $.ajax({
      type: "POST",
      url: "http://localhost:9001/uploadimages",
      data: { 
          imgBase64: dataURL
      }
  }).done(function(o) {
      console.log('all_saved'); 
  
  });
  </code></pre>
  
  <p>And in the node server:</p>
  
  <pre><code>GMserver.post('/uploadimages', function(req, res) {
      var data_url = req.body.imgBase64;
      var matches = data_url.match(/^data:.+\/(.+);base64,(.*)$/);
      var ext = matches[1];
      var base64_data = matches[2];
      var buffer = new Buffer(base64_data, 'base64');
  
      fs.writeFile(__dirname + '/images', buffer, function (err) {
          res.send('success');
          console.log('done');
      });
  });
  
  GMserver.listen(process.env.PORT || 9001);
  </code></pre>
  
  <p>Well, it takes something like 1.5 minutes to complete; and then, even if it says 'done' from node's console and 'all_saved' from browser's console, there isn't any file in the images directory;</p>
  
  <p>anyone knows what I'm doing wrong?</p>