Binary data

HTTP single byte range responses

A byte-range response lets a client request part of a representation rather than the whole body. For an accepted single byte range, HTTP uses status 206 and a Content-Range header identifying the inclusive byte positions and full length. Offsets refer to bytes, not JavaScript string indexes. For a representation of 10 bytes, `bytes=2-5` selects four bytes. Buffer.subarray uses an exclusive end, so add one to the inclusive last position. A valid but unsatisfiable range can receive 416 with `Content-Range: bytes */10`. A server can ignore a Range request and return the full 200 response; multi-range and suffix ranges require additional logic. Our intentionally restricted handler supports only explicit start-end single ranges and ignores other syntax. It serves a fixed uncompressed representation; a production server also handles validators, If-Range, HEAD, conditional requests, file mutation and content negotiation. Never use raw URL segments as filesystem paths or calculate Content-Length from character count. Apply authorization before serving bytes, including partial responses. Do not advertise a toy parser as a full HTTP range implementation. Sources: https://www.rfc-editor.org/rfc/rfc9110.html.

Formal Syntax & Structure

nodejs syntaxnodejs
Range: bytes=start-end; Content-Range: bytes start-end/length

Working Example

Example Snippetnodejs
const data=Buffer.from('0123456789');const start=2,end=5;const result=data.subarray(start,end+1);console.log(result.toString(),result.length);console.log(`bytes ${start}-${end}/${data.length}`);
Node.js Reference