Express.js req.range() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Request

Definition

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)
CallWhat 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.

Library declarations
Express
const express = require("express");
const app = express();

req.range(size)
req.range(size, options)

Parameters

ParameterDescription
sizeRequired. Total size of the resource in bytes. Required so open ranges like bytes=0- can be capped.
optionsOptional. Optional. Only documented option: combine (Boolean, default false) — merge overlapping/adjacent ranges.

Return Value

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);

How It Works

  1. Pass the size. req.range(SIZE) needs the total length so open ranges can be capped.
  2. No header. Missing Range → falsy result → send the full resource with 200.
  3. Errors first. -1 → 416 with Content-Range: bytes */size. -2 → 400.
  4. Valid array. Use ranges.type and each { start, end } to build a partial response (next example).

Example 2 — Send One Byte Range with Status 206

Serve a slice of an in-memory buffer — the usual pattern for simple partial downloads.

Express
const express = require('express');
const app = express();

const BODY = Buffer.from('ABCDEFGHIJKLMNOPQRSTUVWXYZ'); // 26 bytes
const SIZE = BODY.length;

app.get('/letters', (req, res) => {
  const ranges = req.range(SIZE);

  if (!ranges) {
    res.set('Accept-Ranges', 'bytes');
    return res.status(200).send(BODY);
  }
  if (ranges === -1) {
    res.set('Content-Range', `bytes */${SIZE}`);
    return res.status(416).end();
  }
  if (ranges === -2) {
    return res.status(400).end();
  }

  // Most apps support a single range for simplicity
  const { start, end } = ranges[0];
  const chunk = BODY.subarray(start, end + 1);

  res.status(206);
  res.set({
    'Content-Range': `bytes ${start}-${end}/${SIZE}`,
    'Accept-Ranges': 'bytes',
    'Content-Length': chunk.length,
    'Content-Type': 'text/plain; charset=utf-8'
  });
  res.send(chunk);
});

app.listen(3000);

How It Works

  1. Parse against real size. SIZE is 26, matching the buffer length.
  2. Take the first range. ranges[0] gives inclusive start and end.
  3. Slice inclusive. subarray(start, end + 1) because the end index is inclusive in HTTP ranges.
  4. Set headers. 206, Content-Range, Content-Length, and Accept-Ranges: bytes tell the client what was returned.

Example 3 — Combine Overlapping Ranges

Use { combine: true } when the client sends overlapping or adjacent ranges and you want one merged span.

Express
const express = require('express');
const app = express();

app.get('/file', (req, res) => {
  const size = 1000;
  // Example header: Range: bytes=0-99,50-149
  const separate = req.range(size, { combine: false });
  const combined = req.range(size, { combine: true });

  res.json({ separate, combined });
});

app.listen(3000);

How It Works

  1. Default keeps both. With combine: false (default), overlapping ranges stay separate.
  2. Combine merges them. 0-99 and 50-149 become one 0-149 span.
  3. Same API otherwise. Return type is still an array with .type, or -1 / -2 on error.
  4. When to use. Prefer combine: true if you only want to stream continuous byte spans once.

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.

Server Node.js runtime API
Node.js 18+ for Express 5 · 14+ typical for Express 4
Supported
Bun Express-compatible server apps
Supported
Deno Via 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.

Next: req.res

See how the request object links back to the current response.

req.res tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful