2.0.1 • Published 11 years ago
express-range v2.0.1
express-range 
Express middleware for handling Range, Content-Range, and Accept-Ranges headers.
Install
$ npm install express-rangeUsage
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));
});API
range(options)
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, range0-(limit-1)is assumedoptions.length- optional Collection length, orFunction(withfunction(cb(err, length))signature). If not provided, unknown length (*) is assumed.
req.range
POJO, containing the requested range.
req.range.unit-Rangeunitreq.range.first- First items index (defaults to 0 if no range is specified)req.range.last- Last items index (defaults tolimit-1if no range is specified)req.range.suffix- If the range is suffix-style (Range: items=-5- the last 5 items)
res.range(options)
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*)
License
MIT