0

I have a REST API built using node and express. Now i need to send the following data in one http request:

  1. jSON Data
  2. One Audio File to be playable on client

On the client side,i have a simple audio player that requires audio file path as input to play the file. Now i don't understand the whole flow. How will i send the file from API and how the client consume it?

The file is located in file system of the server. Point me in the right direction !!

beNerd
  • 3,088
  • 5
  • 43
  • 84

2 Answers2

0

It is very different type of data you trying to deliver to client.
Much better and scalable will be to have two separate requests. One for JSON data that will contain details over where Audio is located (file name?). RESTful dont have to answer with only JSON or XML data, but it is highly recommended though.

Then another request to node, that will respond with streaming audio data, please check this good question and answers.

If you need just to send audio file without live streaming, then read this: Nodejs send file in response

Community
  • 1
  • 1
moka
  • 21,690
  • 4
  • 48
  • 65
  • in the link above, does the radio-stream module stable enough yet? Another thing is that does the URL path actually points to the mp3 file located on server? – beNerd Jul 05 '13 at 14:11
  • Regarding radio-stream, it looks like haven't been updated for 3 years. But if you need to stream radio, you can encode stream from your audio input and just pipe it to HTTP response. Regarding files, you can do 'static' in express, or you can manually stream files and find them using `fs` your self. At the end, remember that browser will talk through HTTP with node.js process and there is no way accessing directly through HTTP file system, unless you implement it your self. – moka Jul 05 '13 at 15:46
  • i just need to play a audio file in the application. Is it a wise way to just send the audio file url in json response and feed the same to the audio player?? – beNerd Jul 05 '13 at 16:24
  • You need to implement path to your file based on request in node. Please read this to send audio files: http://stackoverflow.com/questions/10046039/nodejs-send-file-in-response Or you can simply have folder called `audio` in public folder, put all files there and then add to express: `app.use('/audio', express.static(%pathToAudioFolder%));` – moka Jul 05 '13 at 16:29
0

Express doesn't appear to support multipart responses. I'd instead recommend returning JSON that includes a URL to the audio file to play. Different routes on your Express server can send the JSON and the audio files. This approach will require two different HTTP requests from your client, but it will also be far more compatible with different browsers, since not all of them deal with HTTP multipart responses the same.

Dan Kohn
  • 31,010
  • 8
  • 77
  • 99
  • i got a bit clue. I just need to play the audio file and not necessarily receive it. So does it makes sense to just return the url of audio file and pass it to the audio player? – beNerd Jul 05 '13 at 15:04
  • I am trying exactly in this way i.e. sending the url of the audio file to my emulator and click it to play. But the problem is the audio file doesn't play. – Deepak Bhatta Aug 29 '17 at 16:09