title: in express js how to read content from a file that i get as a response from another api and store the content in a variable?
body: <p>there is an api endpoint - <a href="https://lichess.org/game/export/q7ZvsdUF" rel="nofollow noreferrer">https://lichess.org/game/export/q7ZvsdUF</a>.</p>
  <p>if you open this api in browser, it downloads a file with the extension .pgn</p>
  <p>i have an express js server. i am hitting this endpoint using the node-fetch library. i need to read the content of this file and store it in a variable as string. how do i read the content of this file?</p>
  <p>these are the headers of the response for hitting this api</p>
  <pre><code>server: nginx
  date: Wed, 17 Aug 2022 09:03:04 GMT
  content-type: application/x-chess-pgn
  content-length: 5539
  vary: Origin
  content-disposition: attachment; filename=lichess_pgn_2017.12.28_Lance5500_vs_TryingHard87.q7ZvsdUF.pgn
  access-control-allow-origin: *
  access-control-allow-headers: Origin, Authorization, If-Modified-Since, Cache-Control
  access-control-allow-methods: OPTIONS, GET, POST, DELETE
  strict-transport-security: max-age=63072000; includeSubDomains; preload
  expect-ct: max-age=31536000, enforce
  x-frame-options: DENY
  permissions-policy: interest-cohort=()
  </code></pre>
  <p>thanks in advance.</p>
  <p><strong>UPDATE --- ANSWER</strong></p>
  
  <pre><code>const value = await new Promise((resolve, reject) =&gt; {
     const data = [];
     response.body.on(&quot;data&quot;, (c) =&gt; data.push(c));
     response.body.on(&quot;end&quot;, (_) =&gt; resolve(Buffer.concat(data).toString()));
     response.body.on(&quot;error&quot;, reject);
  });
  </code></pre>
  <p>if response header is <code>application/json</code> then you can do like this <code>await response.json()</code> to get the data. if it is not, you can do the approach above to get the stream converted into a string to use it in a variable.</p>