title: Nodejs: How to send a readable stream to the browser
body: <p>If I query the box REST API and get back a readable stream, what is the best way to handle it? How do you send it to the browser?? (DISCLAIMER: I'm new to streams and buffers, so some of this code is pretty theoretical)</p>
  
  <p>Can you pass the readStream in the response and let the browser handle it? Or do you have to stream the chunks into a buffer and then send the buffer??</p>
  
  <pre><code>export function getFileStream(req, res) {
    const fileId = req.params.fileId;
    console.log('fileId', fileId);
    req.sdk.files.getReadStream(fileId, null, (err, stream) =&gt; {
      if (err) {
        console.log('error', err);
        return res.status(500).send(err);
      }
      res.type('application/octet-stream');
      console.log('stream', stream);
      return res.status(200).send(stream);
    });
  }
  </code></pre>
  
  <p>Will ^^ work, or do you need to do something like:</p>
  
  <pre><code>export function downloadFile(req, res) {
    const fileId = req.params.fileId;
    console.log('fileId', fileId);
    req.sdk.files.getReadStream(fileId, null, (err, stream) =&gt; {
      if (err) {
        console.log('error', err);
        return res.status(500).send(err);
      }
      const buffers = [];
      const document = new Buffer();
      console.log('stream', stream);
      stream.on('data', (chunk) =&gt; {
        buffers.push(buffer);
      })
      .on('end', function(){
        const finalBuffer = Buffer.concat(buffers);
        return res.status(200).send(finalBuffer);
      });
    });
  }
  </code></pre>