title: Download a dynamic CSV file as an attachment via Express.js
body: <p>When a GET request is sent to a certain API endpoint, CSV string is dynamically created to be served as file attachment.</p>
  
  <p>I have tried to do the following:</p>
  
  <pre><code>const agesMap = 'person1,23\nperson2,32\nperson3,21 ...';
  res.setHeader('Content-type', 'text/csv');
  res.setHeader('Content-Disposition', 'attachment; filename=ages.csv');
  res.write(agesMap);
  res.end();
  </code></pre>
  
  <p><strong>Unfortunately file download wasn't triggered, but instead it was just a response to the original request.</strong></p>
  
  <p>The Request Headers:</p>
  
  <pre><code>Accept:application/json, text/plain, */*
  Accept-Encoding:gzip, deflate, sdch, br
  Accept-Language:en-US,en;q=0.8,ru;q=0.6
  Cache-Control:no-cache
  Connection:keep-alive
  Pragma:no-cache
  </code></pre>
  
  <p>The Response Headers:</p>
  
  <pre><code>Cache-Control:no-store, no-cache, must-revalidate, proxy-revalidate
  Connection:keep-alive
  Content-Disposition:attachment; filename=ages.csv
  Content-Encoding:gzip
  Content-type:text/csv
  Pragma:no-cache
  Surrogate-Control:no-store
  Transfer-Encoding:chunked
  Vary:Origin, Accept-Encoding
  X-Content-Type-Options:nosniff
  X-DNS-Prefetch-Control:off
  X-Download-Options:noopen
  X-Frame-Options:SAMEORIGIN
  X-XSS-Protection:1; mode=block
  </code></pre>
  
  <p><strong>What am I doing wrong?</strong></p>