purposeindustries / express-range

15 stars 1 forks source link

express-range Build Status

Express middleware for handling Range, Content-Range, and Accept-Ranges headers.

Install

Install the package with npm:

$ npm install express-range

Usage

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.

req.range

POJO, containing the requested range.

res.range(options)

Set custom response headers (Content-Range). By default, the middleware sets the same response range, that was requested.

License

MIT