Equivalent of res.download express in restify

Asked

Viewed 24 times

-3

I would like to send a file to the browser to download

In express I do so:

app.get('/apk', (req, res) => {
  res.contentType('application/vnd.android.package-archive');
  res.download(path.join(__dirname, 'apk/apkName.apk'), apkName, (err) => {
      console.log("ERRO? ", err);
      res.send("ERRO! ", err);
  });
});

What it would be like in restify?

  • I believe that there is no equivalent method, maybe you yourself need to create a function that reads the file and make the stream to the front.

  • 1

    I tried to do a function, but I couldn’t... But now I did. I’ll answer

1 answer

1

I found the solution. that would be:

server.get(`/download`, (req, res) => {
  const path = `${process.cwd()}/apk/apkName.apk`;
  res.setHeader('Content-type', 'application/vnd.android.package-archive');
  res.setHeader('Content-Disposition', `attachment; filename=apk/apkName.apk`);

  const readStream = fs.createReadStream(path);
  readStream.pipe(res);
  return res;
});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.