Nodejs - How to stream audio file buffer data up to nodejs server

Viewed 1060

I'm currently build an audio recording application with React Native and using Express as a server. What I'm trying to do is stream the audio file up to the server and then store it in an s3 bucket while also sending the link back down to the client.

At the moment I'm getting the chunks from the buffer which is sent from a post (see below). See below. I'm trying to first just create an mp3 file, this is happening however the audio isn't...nor is anything.

My Question is: How can I stream the data up to the server then create an audio file?

Thanks for your help:

// Post

export const audioStream = async (chunkData) => {
  console.log(chunkData);
  try {
    const data = await axiosAPI.post('/audio', {
      chunkData
    });
  } catch (error) {
    console.log(error)
  }
};

// Data being received in server

{
  chunkData: {
    type: 'Buffer',
    data: [
        1,   0, 255, 255, 231, 255,  18,   0, 255, 255, 240, 255,
       30,   0,  17,   0, 231, 255,  18,   0,   5,   0, 252, 255,
      255, 255, 255, 255,   5,   0,   4,   0,  14,   0, 245, 255,
      255, 255,  18,   0, 248, 255, 244, 255,  12,   0,   7,   0,
        1,   0, 254, 255, 245, 255,  18,   0,   3,   0, 251, 255,
      247, 255,   0,   0,  15,   0, 251, 255, 251, 255, 249, 255,
       10,   0,   5,   0, 244, 255,   1,   0,   2,   0,  17,   0,
      251, 255,   3,   0,  12,   0, 244, 255,  13,   0, 238, 255,
      243, 255,   3,   0,
      ... 1948 more items
    ]
  }
}

// Audio Controller

const fs = require('fs');

async function audioUploadStream(req, res) {
  const myWriteStream = fs.createWriteStream(__dirname + '/testAudio.mp3');

  myWriteStream.on("data", function (chunk) {
    console.log("new chunk received");
    myWriteStream.write(chunk);
  });

  return res.status(200).send({message: "streaming..."})
};

module.exports = {audioUploadStream}

// Audio Router

const express = require("express");
const router = express.Router();

// Controller
const {audioUploadStream} = require('./../controllers/audioController');

// @route api/upload
// @desc Upload image

router.post("/", audioUploadStream);

module.exports = router;

0 Answers
Related