0

I'm using node.js and express and I want to download a current file that is in a specific directory in my server side.

I tried as follows. There is no error but there is no file that is downloaded in my disk too.

What is my wrong?

app.get('/getFile', function(req, res){
  res.download('public/example.pem');
});

Also I tried that too

app.get('/getFile', function(req, res){
  var filename = path.basename('public/example.pem');

    res.setHeader('Content-Disposition', 'attachment; filename=' + filename);
    res.setHeader('Content-Type', 'application/x-pem-file');

    var filestream = fs.createReadStream('public/example.pem');
    filestream.pipe(res);
});
Cansel Muti
  • 598
  • 2
  • 9
  • 22
  • seems like duplicated please take a look here https://stackoverflow.com/questions/11944932/how-to-download-a-file-with-node-js-without-using-third-party-libraries – Ilan Jan 17 '19 at 09:49
  • Yeah, I saw that but the responses in there didn't work for me. Thank you @Ilan – Cansel Muti Jan 17 '19 at 11:01
  • 1
    ok. what about to serve the file as static resources? you can start from this option first... – Ilan Jan 17 '19 at 11:04
  • I guess, you're right. I can serve the file as you mentioned – Cansel Muti Jan 17 '19 at 12:33

0 Answers0