Serve pdf files using node js & express

All PDF files are saved in the file system on the server, how to make the files downloadable on the 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; }); }); 

- 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); }); } 
+4
source share
1 answer

You can use express.static , install it at the beginning of your application:

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

And it will automatically do the job for you when the browser goes to, for example. '/pdf/fooBar.pdf'.

+5
source

Source: https://habr.com/ru/post/1493168/


All Articles