title: Stream file through SFTP connection to client
body: <p>I'm writing a Node Express server which connects via sftp to a file store. I'm using the <a href="https://github.com/jyu213/ssh2-sftp-client" rel="nofollow noreferrer">ssh2-sftp-client</a> package. </p>
  
  <p>To retrieve files it has a <code>get</code> function with the following signature:</p>
  
  <pre><code>get(srcPath, dst, options)
  </code></pre>
  
  <p>The <code>dst</code> argument should either be a string or <code>a writable stream, which will be used as the destination for a stream pipe</code>. </p>
  
  <p>I would like to avoid creating a file object at my server and instead transfer the file onto my client to save memory consumption as described in this <a href="https://medium.freecodecamp.org/node-js-streams-everything-you-need-to-know-c9141306be93" rel="nofollow noreferrer">article</a>. I tried to accomplish this with the following code:</p>
  
  <pre><code>const get = (writeStream) =&gt; {
      sftp.connect(config).then(() =&gt; {
          return sftp.get('path/to/file.zip', writeStream)
      });
  };
  
  app.get('/thefile', (req, res) =&gt; {
      get(res); // pass the res writable stream to sftp.get   
  });
  </code></pre>
  
  <p>However, this causes my node server to crash due to a unhandled promise rejection. Is what I am attempting possible? Should I store the file on my server machine first before sending to the client? I've checked the documentation/examples for the sftp package in question, but cannot find an example of what I am looking for. </p>