Express middleware for handling Range
, Content-Range
, and Accept-Ranges
headers.
$ npm install express-range
Create middleware:
var app = express();
app.use(range({
accept: 'items',
limit: 10,
}));
Uses sane defaults:
var items = [{
name: 'foo',
id: 1
}, {
name: 'bar',
id: 2
}, {
name: 'baz'
}];
app.get('/foo', function(req, res) {
res.range({
first: req.range.first,
last: req.range.last,
length: items.length
});
res.json(items.slice(req.range.first, req.range.last + 1));
});
Creates an express
middleware. The middleware parses the range Range
header, and sets response code 206
if present. It also sets the Accept-Ranges
header.
options.accept
- accepted range unit(s)options.limit
- optional If range not specified in the request, range 0-(limit-1)
is assumedoptions.length
- optional Collection length, or Function
(with function(cb(err, length))
signature).
If not provided, unknown length (*
) is assumed.POJO, containing the requested range.
req.range.unit
- Range
unitreq.range.first
- First items index (defaults to 0 if no range is specified)req.range.last
- Last items index (defaults to limit-1
if no range is specified)req.range.suffix
- If the range is suffix-style (Range: items=-5
- the last 5 items)Set custom response headers (Content-Range
). By default, the middleware sets
the same response range, that was requested.
options.unit
- Specify range unit (it defaults to the requested unit)options.first
- Specify the first items index (it defaults to the requested one)options.last
- Specify the last items index(it defaults to the requested one)
. options.length
- Specify the resource lenth (it defaults to middleware default, or *
)MIT