Home > Enterprise >  How to handle blob in express(nodejs)
How to handle blob in express(nodejs)

Time:10-06

I've got google extension, react frontend app and express server. I use mediaRecorder to record my screen and insert it into frontend page.There is no problem, video works just fine in frontend

const blob = new Blob(chunks, { type: "video/mp4;" });
const savedVideo = document.getElementById("savedVideo");
chunks = [];

const videoURL = window.URL.createObjectURL(blob);
savedVideo.src = videoURL;

var tracks = stream.getTracks();
tracks[0].stop();

let response = await fetch('http://localhost:3001/upload', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/octet-stream',
  },
  body: blob
});

The problem starts when i send blob to server.I want to save video(Only on server side)I suppose problem is in handling blob on the server side, maybe I doing smth wrong, here my server code:

const express = require("express");
const cors = require('cors');
const fs = require('fs');

const app = express();
const port = 3001;

app.use(cors({
  origin: 'http://localhost:3000'
}));

app.post("/upload", (req, res) => {
  console.log('req.body', req.body)
  req.on('readable', function(){
    const data = req.read();
    if(data) {
      fs.createWriteStream('videeoo.mp4').write(data);
      // also i didnt sure about this method to write file
    }
    console.log('data', data);
  });        
});

app.listen(port, () => {
  console.log(`Server started at http://localhost:${port}`);
});

express logs

I'am waiting your best practices)Grasias!

CodePudding user response:

well... this is problematic... the req.read() doesn't normally process binary data. There's also a conceptual issue here: a video can potentially be huge, but in your application you're waiting for the whole file to be uploaded before you start writing it. So if you have 10 users, each uploading 10GB files, this is a problem. So you really want to store the file as it arrives, so that you only keep a few bytes in your memory at a time... but then what if you want to limit the size of the file? probably 10GB files is not something you want to deal with?

So... there are really a lot of corner cases and things to consider. In general, you don't want to handle these things manually. Luckily there are libraries like multer that can handle all these issues for you: https://expressjs.com/en/resources/middleware/multer.html you just define the destination directory, the max file size, etc and the library takes care of everything for you

  • Related