0

I am new to js. I am trying to display a pdf file in browser, however I am continuously getting the same respond 'Cannot GET..."

I've tried it different ways.

router.get("/en/tc", function(req, res){
    // res.sendFile("/assets/downloads/TC_TS_eng.pdf", {root: "."});
    res.download("./assets/downloads/TC_TS_eng.pdf");
})

As well as via 'fileSend'.

It all works well as long as I run it locally. However as soon as I move it to the server it starts returning the above mentioned response. Any help will be highly appreciated.

Anton.V
  • 11
  • 1
  • i think your question is answered in this [thread](https://stackoverflow.com/questions/31105846/how-to-send-a-pdf-file-from-node-express-app-to-the-browser#31106110) – zouhair Aug 27 '20 at 12:08

1 Answers1

0

You could do this:

app.get("/", function(req, res){

  res.sendFile(__dirname + "/test.pdf");

});

What I did here is quite simple. I sent the file when the user goes to the root directory (/) and send the pdf file. __dirname is directory name, which basically makes sure that even when you host on, say heroku, you can still get the file beacuse it gets the full directory path to the file, and then + the pdf name. Hope this helps! Good luck.