title: NodeJs: binary fs.createReadStream streamed as UTF-8
body: <p>My task at hand was to read a jpeg file in NodeJs and send it as http-response to answer a server request. Seemed to be trivial. However, my first solution failed. The browser did receive some binary gibrish, that was about 30% larger than the original file.</p>
  
  <p>My code was (simplified; res is of type SeverResponse):</p>
  
  <pre><code>...
  var fs = require('fs');
  var stream = fs.createReadStream(pathToJPEG, {encoding: 'binary'});
  res.setHeader('Content-Type', "image/jpeg");
  stream.pipe(res);
  ...
  </code></pre>
  
  <p>As it turned out, what arrived at the browser was the UTF-8 encoded version of my source data. I also was able to exlude the response object to be the culprit. When I gave it an alternative stream (from Buffer, not file) it worked just fine.</p>
  
  <p>Turns out the solution to my problem was to drop the option {encoding: 'binary'}. With that my browser received the right picture:</p>
  
  <pre><code>...
  var fs = require('fs');
  var stream = fs.createReadStream(pathToJPEG);
  res.setHeader('Content-Type', "image/jpeg");
  stream.pipe(res);
  ...
  </code></pre>
  
  <p>My question is: Why? </p>
  
  <p>It seems intuitive that the first non-working version should be the correct one since it explicitly declares how to read the file.</p>