问题
I am creating a music web app that streams MP3s that I have stored in MongoDB(GridFS).
My Question: How can I add http range support so that I can start streaming the audio file 1/2 of the way through without having to wait for the buffer.
I know GridFS supports reading for X bytes - X bytes, so basically I just need to know how to get nodejs to understand it only needs bytes X - X.
Thanks!
回答1:
The client will send a Range
header specifying the absolute starting and ending bytes followed by 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
The server should then return a response code 206
(Partial content) and the Content-Length
should be only the amount of data transmitted.
In the case the range is wrong, the server should either return 416
(Requested range not satisfiable) with a Content-Range
field of bytes */*
or should ignore the range request and return a 200
with the entire body of the file.
The server must also send an Accept-Ranges
field with the value of the accepted range unit, in this case bytes
. But the range unit can be any custom range unit you want.
Source: rfc2616
来源:https://stackoverflow.com/questions/8696523/nodejs-http-range-support-partial-file-download