JSPM

express-range

2.0.1
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 312
  • Score
    100M100P100Q87263F
  • License MIT

Express REST middleware for Content-Range pagination

Package Exports

  • express-range

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (express-range) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

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.

  • options.accept - accepted range unit(s)
  • options.limit - optional If range not specified in the request, range 0-(limit-1) is assumed
  • options.length - optional Collection length, or Function (with function(cb(err, length)) signature). If not provided, unknown length (*) is assumed.

req.range

POJO, containing the requested range.

  • req.range.unit - Range unit
  • req.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)

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