I'm writing a Node Express server which connects via sftp to a file store. I'm using the ssh2-sftp-client package.
To retrieve files it has a get
function with the following signature:
get(srcPath, dst, options)
The dst
argument should either be a string or a writable stream, which will be used as the destination for a stream pipe
.
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 article. I tried to accomplish this with the following code:
const get = (writeStream) => {
sftp.connect(config).then(() => {
return sftp.get('path/to/file.zip', writeStream)
});
};
app.get('/thefile', (req, res) => {
get(res); // pass the res writable stream to sftp.get
});
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.