title: How can I save a base64-encoded image to disk?
body: <p>My Express app is receiving a base64-encoded PNG from the browser (generated from canvas with toDataURL() ) and writing it to a file. But the file isn't a valid image file, and the "file" utility simply identifies it as "data".</p>
  
  <pre><code>var body = req.rawBody,
    base64Data = body.replace(/^data:image\/png;base64,/,""),
    binaryData = new Buffer(base64Data, 'base64').toString('binary');
  
  require("fs").writeFile("out.png", binaryData, "binary", function(err) {
    console.log(err); // writes out file without error, but it's not a valid image
  });
  </code></pre>