title: get image from another domain and encode base64 by node js
body: <p>I want get a image from url and encode base64 that image by nodejs, then show that image by base64 encode, but this code is incorrect.
  this code save incorrect png file. </p>
  
  <pre><code>var http = require('http')
    , fs = require('fs')
    , options
  
  options = {
      host: 'google.com'
    , port: 80
    , path: '/images/srpr/logo3w.png'
  }
  
  function base64_encode(bitmap) {  
      return new Buffer(bitmap).toString('base64');
  }
  
  function ImageReady(res2){
      var request = http.get(options, function(res){
          var imagedata = '';
          res.setEncoding('binary');
  
          res.on('data', function(chunk){
              imagedata += chunk;
          })
  
          res.on('end', function(){
              var base64encode = base64_encode(imagedata);
              res2.end('&lt;img src="data:image/png;base64,'+base64encode+'" /&gt;');
              fs.writeFile('logo.png', imagedata, 'binary', function(err){
              if (err) throw err
              console.log('File saved.')
          })
          })
  
      })
  }
  
  var httpListen = require('http');
  httpListen.createServer(function (req, res) {
      res.writeHead(200, {'Content-Type': 'text/html'});
      ImageReady(res);
  }).listen(8080);
  console.log('Server running!');
  </code></pre>