title: Writing binary data using node.js fs.writeFile to create an image file
body: <p>I'm trying to write a canvas data with node.js <a href="https://nodejs.org/api/fs.html#fs_fs_writefile_file_data_options_callback" rel="noreferrer"><code>fs.writeFile</code></a> as a binary. JPEG file, but after the file is written I can see that the file is stored as plain text, not binary data.</p>
  
  <p>This is an example of the <code>data</code> sent from the client to my node, representing the JPEG image data (just a few first characters):</p>
  
  <p><code>/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAFA3PEY8MlBGQUZaVVBfeM...</code></p>
  
  <p>I'm getting this <code>data</code> <strong>on the client side</strong> by performing:</p>
  
  <pre><code>canvas.toDataURL('image/jpeg', 0.5).replace('data:image/jpeg;base64,', '')
  </code></pre>
  
  <p>Here is the function usage in my <strong>node.js server</strong>:</p>
  
  <pre><code>fs.writeFile('../some.jpeg', data, 'binary', function(err){});
  </code></pre>
  
  <p>Instead of the file being written as binary (<code>״״ JFIF  ...</code>), it writes exactly the data it received from the client.</p>
  
  <p>What am I doing wrong here?</p>