0

I am creating a web application using node js that can download videos from facebook, i am getting the url and quality using express using the code below but how can i download it

const express = require('express');
const app = express();
const path = require('path');
const bodyParser = require('body-parser');
var http = require('http');
var fs = require('fs');

app.get('/', (req,res)=>{
    res.sendFile(path.join(__dirname,'templates','index.html'));  
});

app.use(bodyParser.urlencoded({ extended: true })); 

//app.use(express.bodyParser());

app.post('/send_data', function(req, res) {
  res.send('You sent the name "' + req.body.fbUrl + ' in '+req.body.quality+' Quality".');
  if(req.body.quality == "HD")
  {
    download_video("HD");
  }
  else if(req.body.quality == "SD")
  {
    download_video("SD");
  }
  else if(req.body.quality == "MP3")
  {
    download_video("MP3");
  }
  else
  {
    app.get('/', (req,res)=>{
        res.sendFile(path.join(__dirname,'templates','index.html'));  
    });
  }
  function download_video(quality)
  {
      console.log('video is downloading in "'+req.body.quality+'" Quality');
  }
Umair Malik
  • 35
  • 1
  • 1
  • 8
  • 1
    First of all, by using **res.send(...)** at the top of you function will break the http request/response flow like if it was a return so I'd use it after all my stuff is done. On the other hand if you want to download a Facebook Video you have to use its [API](https://developers.facebook.com/docs/graph-api/reference/insights#example). I think the following [link](https://stackoverflow.com/questions/8642579/how-can-i-download-a-video-from-facebook-using-graphapi) is not what you want to do at all, but it might be useful. – Jose Vasquez Aug 31 '20 at 08:20

1 Answers1

1

I don't know how are you getting the FB video URL explicitly. However, I can help you with how to download video from URL,

let http = require('http');
let fs = require('fs');

let download = (url, dest, cb) => {
  let file = fs.createWriteStream(dest);
  http.get(url, function(response) {
    response.pipe(file);
    file.on('finish', function() {
      file.close(cb);
    });
  });
}

This will create a file stream and download the file chunk by chunk to the destination path (dest).

Vipul Patil
  • 1,250
  • 15
  • 27