What I'm trying to do is upload a webcam or audio stream to Nodejs and save it to disk. The goal is to send the data chunks to the server as soon as the data becomes available. I've grabbed the stream from gerUserMedia, setup mediaRecorder and when data is available, I turn the blob into a arrayBuffer and send it to the server. I know the code is wrong as I'm not sure what the proper way is, but when the data is sent to the server, a file is saved, but only 1kb file, where it should be a large video file. How can I do this?
//Client Side
recorder.ondataavailable = (event) => {
const reader = new FileReader();
reader.onload = (event) =>{
recordedChunks.push(event.data);
sendDataToServer(event.target.result)
}
reader.readAsArrayBuffer(event.data)
}
reader.readAsArrayBuffer(event.data)
//Server-Side
app.post('/media', function (req, res, next) {
try {
const { data } = req.body;
const dataBuffer = new Buffer(data, 'base64');
const fileStream = fs.createWriteStream('video.webm');
fileStream.write(dataBuffer);
} catch (error) {
console.log(error);
}
})