Send audio from Node backend to frontend - node.js

I have Node/Express server set up in which several thousand audio files are stored within the src folder. I currently have a route set up that sends JSON data in the response object. I wish to also send an mp3 audio file that can be parsed at the front end and be converted to an audio object. I cannot figure out how to encode the audio file such that it may be sent. I have looked into Blobs (these don't seem to be possible in Node) and converting the binary file into a string that may be sent as part of the response body.
Any ideas as to how this may be possible?

You can directly send the audio file with something like:
res.sendFile(__dirname, "/src/audioFile.mp3");
Or you could Base64 encode the audio file for your frontend to parse:
fs.readFile("./src/audioFile.mp3", function(err, result) {
res.send(result.toString("base64"));
});

Related

Send metadata with file as a response in Sanic Server Python

I want to send an audio file, along with some other metadata text (like server details, etc.), as a response in the Sanic server using Python.
I know that we can send, responses in text, JSON, or as a file. But would like to know, is there any way we can send the file as well as some other datatype as a response in a single request?
I tried with response.json by sending the metadata, as well as audio data (converting bytes to string), but while converting to string, I feel, some of the metadata of the audio is lost.
Would like to know, is there any effective way to send files, as well as some other metadata using Sanic in a single request?
Thanks :)
I just tried to do a little hack... I send the metadata in the header and stream the file as usual (response.file()), so from the client side, I read the metadata from the header, and get the file as resp
example:
headers = {
'metadata': text,
}
return await response.file(audio_file, headers=headers)

Nestjs multer upload file after some codes

I am developing an api based on nestjs. I used multer package to upload file. The code sample on nestjs documentation is at the following:
#Post('upload')
#UseInterceptors(FilesInterceptor('files'))
uploadFile(#UploadedFiles() files: Array<Express.Multer.File>) {
console.log(files);
}
But I want to save uploaded file after send mail. If the mail sends successfully then I will save the file. If sending mail process is fail, I ignore the file uploding.
How can I figure out?
You could do this in two ways, One is to create another route like#Post('mail') then depending on the response you receive in your client, let's say it returns an OK you can send another request to upload the files, or you can send both the requests to your API at the same time then cancel uploading the files (supposing this request takes longer to complete) if sending the mail was not successful (for this you need to handle errors that might result in incomplete files in your API, basically your API should know the full size of the files to expect so that you can compare if there was no cancellation).
The other way is using one route to do both of the tasks, in your example add the code for handling sending the mail and based on the condition that it went successfully do or don't proceed with uploading the files.

Multiparty read uploaded file content

How can I get uploaded file content using multiparty in node.js? I don't need temp file, I need to redirect all stream into Google Cloud Storage in order to save the file content, but I can't find the way to get this content with events.
Found the answer. We need to use part stream a subscribe to standard streams events, like, data and end in order to receive file's data.
part.on("data", chunk => {
writeStream.write(chunk);
});
part.on("end", chunk => {
writeStream.end(chunk);
});
writeStream - is a another stream where you want to put your data. In my case that was Google Cloud Storage file PUT request via signedUrl.
part - is a part object of a form part event

Image uploading - How to get the right format of image data for server side processing

I'm using sharp to process images on the server side and react dropzone to get the image file. When I post the file to the server from dropzone, I get the blob out of the request.body that looks like:
{ preview: 'blob:http%3A//127.0.0.1%3A3000/1451580a-b24f-478f-a9f7-338c0d790829' }
Optionally, before I send data to the server I can use FileReader (or something else) to do something with the image file instead of turning it into a blob.
Sharp takes:
A Buffer containing JPEG, PNG, WebP, GIF, SVG, TIFF
Raw pixel image data
A String containing the path to an image file, with most major formats supported.
How can I use what I have to provide sharp a supported format?
I recommend trying out a node.js module called Multer to help you access your photo file on your server.
https://github.com/expressjs/multer
First, on the client, you'll want to append your file to a FormData object like this:
// obtain the file from your react dropzone and save it to this file variable
const file = dropzone.file // not sure how you do this with react dropzone
const data = new FormData()
data.append('photo', file)
Then you'll send this FormData object to your server. On the server you'll use Multer on the route you're using for the photo for processing.
Make sure you npm install multer, and require it on your server or routes file. If you're sending a single file you'll use the multer 'single' method. If you want to do anything different check out the API documentation.
app.post('/photos', multer().single('photo'), controller.processPhoto);
In this example route, you're sending a POST request to /photos, multer is looking for a file with a FormData key of 'photo' and appending that to the request object.
Then in this made up 'controller.processPhoto' method you'll have access to the image as a property of the request object, on req.file. With this you can easily access a lot of good information including the image buffer req.file.buffer which it sounds like you need. (also mimetype, original name etc.)
This should be enough to get you started.

How to receive a zip file in koa server

I'm implementing a webserver using Koa. One of my request handlers needs to receive and store a large zip file. The file is NOT uploaded from a web page, but sent from another NodeJs application. Therefore it's not multipart encoded, but just applcation/octet-stream. How can I read such a file from the request?
I've noticed there is a request.socket object but I could not find any documentation on how to use it.
In other words I need the opposite to
this.body = fs.createReadStream(path);

Resources