buffer to base64 image nodejs code example

Example 1: node js download image from url as buffer

var request = require('request').defaults({ encoding: null });
request.get(s3Url, function (err, res, body) {
      //process exif here
});

Example 2: buffer to image nodejs

function toBase64(arr) {
   //arr = new Uint8Array(arr) if it's an ArrayBuffer
   return btoa(
      arr.reduce((data, byte) => data + String.fromCharCode(byte), '')
   );
}

$('#two').prepend($('<img>',{id:'theImg2',src:`data:image/png;base64,${toBase64( selected[0].image2.data)}`}))