title: how to decode base64 to image in Nodejs?
body: <p>I'm sending an image encoded as base64 through sockets and decoding is not working. The file that must contain the new image is written as base64 instead of a jpg file.</p>
  <p>encoding socket:</p>
  <pre><code>function encode_base64(filename) {
    fs.readFile(path.join(__dirname, filename), function (error, data) {
      if (error) {
        throw error;
      } else {
        console.log(data);
        var dataBase64 = data.toString('base64');
        console.log(dataBase64);
        
  
        client.write(dataBase64);
      }
    });
  }
  
  rl.on('line', (data) =&gt; {
      encode_base64('../image.jpg')
  
  })
  </code></pre>
  <p>decoding socket:</p>
  <pre><code>function base64_decode(base64str, file) {
    
     var bitmap = new Buffer(base64str, 'base64');
     
     fs.writeFileSync(file, bitmap);
     console.log('****** File created from base64 encoded string ******');
    }
  
  
  client.on('data', (data) =&gt; {
  
  
      base64_decode(data,'copy.jpg')
  
    
  });
  
  // the first few characters in the new file 
  //k1NRWuGwBGJpmHDTI9VcgOcRgIT0ftMsldCjFJ43whvppjV48NGq3eeOIeeur
  </code></pre>