Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

serve pdf files using node js & express

All the PDF files are saved in the filesystem on the server, how to make the files to be downloadable in client side.

for Ex :

 app.use('/pdfDownload', function(req, res){
  var pathToTheFile = req.body.fileName;
   readFile(pathToTheFile, function(data){
      //code to make the data to be downloadable;
    });
 });

is the request made

function readFile(pathToTheFile, cb){
   var fs = require('fs');
   fs.readFile(pathToTheFile, function(err, data){
      //how to make the file fetched to be downloadable in the client requested
      //cb(data);
   }); 
}
like image 837
Beast Avatar asked Jul 24 '13 10:07

Beast


1 Answers

You can use express.static, set it up early in your app:

app.use('/pdf', express.static(__dirname + '/pathToPDF'));

And it will automatically do the job for you when browser navigates to e.g. '/pdf/fooBar.pdf'.

like image 140
moka Avatar answered Oct 06 '22 18:10

moka