2

Can you please help me in create a Video URL Blob in NodeJS?

    var xhr = new XMLHttpRequest();
    xhr.responseType = 'blob';

    xhr.onload = function() {
        var reader = new FileReader();

        reader.onloadend = function() {
            var byteCharacters = atob(reader.result.slice(reader.result.indexOf(',') + 1));

            var byteNumbers = new Array(byteCharacters.length);

            for (var i = 0; i < byteCharacters.length; i++) {
                byteNumbers[i] = byteCharacters.charCodeAt(i);
            }

            var byteArray = new Uint8Array(byteNumbers);
            var blob = new Blob([ byteArray ], { type: 'video/ogg' });
            var url = URL.createObjectURL(blob);

            console.log(url);
        };

        reader.readAsDataURL(xhr.response);
    };

    xhr.open(
        'GET',
        '...Video URL...'
    );
    xhr.send();

Error Output:

  throw new Error('cannot read as File: ' + JSON.stringify(file));   
  Error: cannot read as File: undefined

I have used the packages XMLHttpRequest, URL, FileReader and Blob

Please Help, Thanks

2 Answers 2

1

Node.JS does not have XMLHttpRequest, FileReader, Blob, etc. Instead, it uses the fs, http modules and other built-in classes, like Buffer

Trying to use these browser features on the server, even if using packages that try to emulate them, may not always work. In fact, many of those libraries which emulate those features do not include everything. For example, the xmlhttprequest node package does not use .response, but only the text response.

In Node.JS, there is no need for blob URLs (where would you even use that?), you just interact with the Buffers directly.

// making a http request to fetch something
const http = require("https");

const request = https.request({
  method: "GET",
  url: "https://example.com/video.ogg"
}, (res) => {
  const chunks = [];

  res.on("data", (chunk) => {
    chunks.push(chunk);
  });

  res.on("end", () => {
    // raw byte data
    const buffer = Buffer.concat(chunks);

    /*
     * Interact with Buffer here
     */

  });
});

request.end();
Sign up to request clarification or add additional context in comments.

5 Comments

Error coming: 'errno: 'ECONNREFUSED', code: 'ECONNREFUSED', syscall: 'connect', address: '127.0.0.1', port: 443'
I just wanted to make an app like (YouTube) where anybody can upload their own videos, So, I need Blob to make the video accessible through the website only. I want to make the blob in the format 'blob:localhost:8080/10dcd82b-a53d-4799-82ee-0aaaa1a889d5' in the backend with node.js ( as the frontend exposes everything! ). So, I need to create a blob in the backend with node.js and pass the result blob to the frontend and display the video
Blob URLs cannot be made in the backend. Blob URLs are tied to the document/web page it was created on and are removed when the tab is closed. They are only accessible by the computer/browser it was made on.
Any Solution to Create a Blob Privately so that the URL is not exposed?
1

You can Use Azure Cloud for storing the videos and then us Azure BLOB

5 Comments

To use Azure Cloud
and Azure Blob.
I not using Azure Cloud
But Using Google Cloud
TO create blobs

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.