Nodejs - HTTP Range Support / Partial File Upload

I am creating a music web application that transfers MP3 files that I saved to MongoDB (GridFS).

My question is: how can I add support for the HTTP range so that I can start streaming the 1/2 audio file without waiting for the buffer.

I know that GridFS supports reading for X bytes - X bytes, so basically I just need to know how to get nodejs to understand that it only needs X - X bytes.

Thanks!

+7
source share
1 answer

The client will send a Range header defining the absolute start and end bytes, followed by the total file length or "*".

Examples:

  . The first 500 bytes: bytes 0-499/1234 . The second 500 bytes: bytes 500-999/1234 . All except for the first 500 bytes: bytes 500-1233/1234 . The last 500 bytes: bytes 734-1233/1234 

Then the server should return a response code 206 (partial content), and the Content-Length should be only the amount of data transferred.

In the case of an invalid range, the server should either return 416 (the requested range is not feasible) with the Content-Range bytes */* field, or ignore the range request and return 200 with the entire body of the file.

The server must also send an Accept-Ranges field with the value of the accepted range range, in this case bytes . But the range block can be any custom range that you want.

Source: rfc2616

+7
source

All Articles