Home > Back-end >  res.sendFile("path") is not accepting firebase storage video URL
res.sendFile("path") is not accepting firebase storage video URL

Time:09-18

I'm working on the video stream web app like youtube in firebase hosting and storage in nodejs.

I'm able to host the video player by getting the firebase storage download url location from client side and can play the video. But I don't want my users to see the url by inspecting the web page. For that I made a app.get("/video/videoID") in nodejs express and getting the download url server side, and I'm able to get the download URL. But I'm not able to send the URL to my video player. I tried res.sendFile("URL path"); but it is saying that the "Path should be absolute". I understand that the path should come from the server local machine.

Is there any way where I can send the URL to my video source path from server?

HTML file:

<video width="100%" controls><source src="/video/songID" type="video/mp4">Your browser does not support HTML video.</video>

Server code:

app.get("/video/:songKey", function(req, res, next) {
    var fDb = admin.database();
    var ref = fDb.ref("videos");
    ref.orderByKey().equalTo(req.params.songKey).on('child_added', async (snapshot) => {
        var refPath = snapshot.val().video_ref_path; // This is the firebase storage ref path stored in realtime database
        var storage = admin.storage();
        var songFile = storage.bucket("<bucket-name>").file(refPath);
        var [meta] = await songFile.getMetadata();
        var token = meta.metadata.firebaseStorageDownloadTokens;
        var link = `https://firebasestorage.googleapis.com/v0/b/<bucket_name>/o/${encodeURIComponent(
            refPath
          )}?alt=media&token=${token}`
        res.sendFile(link); //This line is giving error saying path needs absolute path
        // If I use res.send(link) call the web location I'm able to get the file URL
    });
});

Any help with this is appreciated and thankful. BTW I'm new to nodejs and learning it.

Thanks in Advance

CodePudding user response:

res.sendFile() is meant to send the contents of locally (to the server) stored files.

You probably want to use res.redirect(), which is meant to redirect the browser to another URL.

  • Related