0

I am making a simple website for my grandad so he can download videos from YouTube. To do this, I am using an npm package (ytdl-core), MERN stack, and Heroku.

I have the following code (which it works locally on my computer):

app.post("/download", (req, res) => {
  const { searchBar } = req.body;
  const address = searchBar;
  console.log(address);
  const option = Object.keys(req.body)[1];
  console.log(option);
  let id = address.slice(32);
  if (option === "video") {
    ytdl(address).pipe(fs.createWriteStream('video.mp4'));
    console.log("Downloading!");
  }
  res.redirect("/");
});

How it works: the user (my grandad) pastes the URL of the video into a form with two options (video or audio) and then the code above handles the rest.

Problem: the file is never downloaded.

Remember: it works locally, but as soon as I try it using Heroku it doesn't download anything.

Stigma
  • 1
  • 1

1 Answers1

1

You are not actually downloading the file to the user's computer.

When you do fs.createWriteStream('video.mp4') it opens a file to the computer running nodeJS, which is your own computer on local that might be confusing you. What you are doing is writing the file to Heroku's server when it is on Heroku.

You'd need to send it as a response for your user to download it. Instead of redirecting:

app.post("/download", (req, res) => {
  const { searchBar } = req.body;
  const address = searchBar;
  console.log(address);
  const option = Object.keys(req.body)[1];
  console.log(option);
  let id = address.slice(32);
  if (option === "video") {
    res.attachment('video.mp4');
    ytdl(address).pipe(res);
  }
});
Salketer
  • 14,263
  • 2
  • 30
  • 58
  • I literally copied and pasted your solution, upload the code, and still doesn't work... It simply redirects you to the homepage (which is actually the last step, but nothing downloads...) – Stigma Jul 01 '22 at 12:16
  • I litterally did not test the code at all, just gave you an example of something that should work. But the text above explaining what was happening was, IMO, 10 times more helpful. – Salketer Jul 01 '22 at 12:18
  • Also, if it redirects, this means you did not copy paste correctly. There is no redirect in my code. – Salketer Jul 01 '22 at 12:19
  • Sorry, I really want to redirect the user to the homepage (that's why my last line is (res.redirect("/");) so that's fine. I guess I didn't even stop for a second to really understand what you were trying to explain, I just thought you gave me the solution (and since I have been having this problem for a while now I just hoped it worked). I am going to think about that now, but if by any chance you find the practical solution please share it. Thanks! – Stigma Jul 01 '22 at 12:24
  • You cannot redirect if you want to send a file... You either take the request and REDIRECT it somewhere else, or you HANDLE it by serving the file. – Salketer Jul 01 '22 at 12:28
  • Thank you so much for your help. One last question: can you explain a little bit more how to redirect the user to the homepage once the download is finished? I have seen many websites that do something such as downloading a file and even while downloading they redirect you to another page. That is what I wanted to achieve. That said, I understand that doing this in the way I am trying to achieve it is not correct, but how could I do this? Could you explain a little bit more about this process? Sorry about all these questions, I am quite new – Stigma Jul 01 '22 at 13:17
  • hmmm, you could do it on the front end, but then you'd need to know when the download was complete, and that's not easier. Else you could send the form with ajax, and handle the download in JS so you could know when it is ended and then redirect. The fastests and easiest way would be to explain carefully to your grandpa to press the back button after. :) – Salketer Jul 01 '22 at 13:22
  • Here's a discussion I had yesterday about it https://stackoverflow.com/questions/72812253/is-there-any-way-of-listening-to-download-complete-event-after-download-starts-u/72812482#72812482 – Salketer Jul 01 '22 at 13:22
  • hahaha Yeah, it will probably be easier for me to explain to my grandad that he just needs to press the 'Home' icon to start again XD Thanks again for your help – Stigma Jul 01 '22 at 13:30