title: Convert Base64 image to raw binary with Node.js
body: <p>I have found posts that are close to what I'm looking for, but I have not been able to successfully implement what I want. Here is the general flow:</p>
  
  <ol>
  <li>Submit photo with rest of venue data, as base64 data</li>
  <li>Strip data prefix if it exists, so I have just the image base64 data</li>
  </ol>
  
  <hr>
  
  <pre><code>var base64data = venue.image.replace(/^data:image\/png;base64,|^data:image\/jpeg;base64,|^data:image\/jpg;base64,|^data:image\/bmp;base64,/, '');
  </code></pre>
  
  <hr>
  
  <ol start="3">
  <li>Store Base64 data in GridFS via MongoDB (I'm using <a href="https://github.com/jupe/gridfstore" rel="noreferrer">gridfstore</a>)</li>
  <li>Then, I'd like to retrieve the image upon request as a raw image file via a URL. </li>
  </ol>
  
  <hr>
  
  <pre><code>// generic images route
  server.get(version+'/images/:id', function(req, res) {
    gridfstore.read( req.params.id, function(error,data) {
      res.writeHead(200, {
        'Content-Type': 'image/jpeg',
        'Content-Length': data.buffer.length
      });
  
      res.end(data.buffer);
    });
  });
  </code></pre>
  
  <p>Basically, this method returns the Base64 bytes stored in GridFS. I have tried other methods but they don't return the raw image.</p>
  
  <p>I'd like to pull up the image using URLs like this:</p>
  
  <pre><code>http://[localhost]/1/images/11dbcef0-257b-11e3-97d7-cbbea10abbcb
  </code></pre>
  
  <p>Here is a screenshot of the browser trace:
  <img src="https://i.stack.imgur.com/gJ5Nn.png" alt="browser trace"></p>