req.range(size) parses the HTTP Range header against a known resource size (in bytes). Clients use it for video seeking, resume downloads, and other partial content. Express returns inclusive { start, end } ranges — or an error code.
Use it when you serve files or buffers and need 206 Partial Content. It only parses the header; you still set status, Content-Range, and stream the bytes. Read related headers with req.get().
Syntax
Call req.range in your Express.js app. Start with this line.
Express
req.range(size)
req.range(size, options)
Call
What it does
req.range(size)
Primary signature.
req.range(size, options)
Related call form.
Install with npm install express. Create the app with const app = express() before you use this API in an Express.js tutorial project.
req.range(size[, options]) parses the Range header against a known size and returns ranges, or special sentinel values for invalid/unsatisfiable ranges. It does not send bytes by itself.
Express
const ranges = req.range(stat.size);
if (!ranges) { /* no Range header */ }
else if (ranges === -1) { /* unsatisfiable */ }
else { /* array of { start, end } */ }
Express.js Examples
Example 1 — Parse the Range Header and Branch
Call req.range(size) and handle every return value before touching the body.
Express
const express = require('express');
const app = express();
const SIZE = 1000; // demo resource size in bytes
app.get('/demo', (req, res) => {
const ranges = req.range(SIZE);
if (!ranges) {
return res.status(200).send('Full content (no Range header)');
}
if (ranges === -1) {
res.set('Content-Range', `bytes */${SIZE}`);
return res.status(416).send('Range Not Satisfiable');
}
if (ranges === -2) {
return res.status(400).send('Malformed Range header');
}
res.json({
type: ranges.type,
ranges: ranges.map((r) => ({ start: r.start, end: r.end }))
});
});
app.listen(3000);
Output
GET /demo
→ 200 Full content (no Range header)
GET /demo (Range: bytes=0-99)
→ 200 { "type": "bytes", "ranges": [{ "start": 0, "end": 99 }] }
GET /demo (Range: bytes=5000-6000)
→ 416 Range Not Satisfiable
GET /demo (Range: not-a-range)
→ 400 Malformed Range header
How It Works
Pass the size.req.range(SIZE) needs the total length so open ranges can be capped.
No header. Missing Range → falsy result → send the full resource with 200.
Combine merges them.0-99 and 50-149 become one 0-149 span.
Same API otherwise. Return type is still an array with .type, or -1 / -2 on error.
When to use. Prefer combine: true if you only want to stream continuous byte spans once.
Compatibility
Runtime Support
req.range() is an Express.js request method. It runs on a Node.js server (Express 5 needs Node.js 18+). Browsers only send the Range header; they do not execute req.range().
✓ Server · Express 4 & 5
req.range()
Works wherever Express runs: Node.js is the primary target. Bun and Deno can run many Express apps with compatibility layers. Not available in the browser.
ServerNode.js runtime API
Node.js18+ for Express 5 · 14+ typical for Express 4
Supported
BunExpress-compatible server apps
Supported
DenoVia npm compatibility / adapters
Supported
req.range()Express on Node.js
Bottom line: Study and run these examples with Node.js and the express package. Copy a snippet into a local file and start the server with node.
Frequently Asked Questions
It parses the incoming HTTP Range header against a known resource size and returns byte ranges (or an error code) so you can send 206 Partial Content responses.
An array of { start, end } ranges (with a .type property such as "bytes"), undefined when no Range header is present, -1 when no satisfiable ranges remain, or -2 when the header is structurally malformed (for example missing "=").
When options.combine is true, overlapping and adjacent ranges are merged into larger ranges before the result is returned. The default is false.
No. It only parses the header. You still set status 206, Content-Range, Content-Length, Accept-Ranges, and write or stream the bytes.
No. Express runs on a Node.js (or compatible) server. Browsers send the Range header; your server calls req.range() to parse it.