Very simple and easy to use connect (express) middleware for parsing pagination query strings.
$ npm install connect-pagination
var connect = require('connect'),
pagination = require('connect-pagination')
var paginationOptions = {
pageParam: 'p'
};
connect()
.use(pagination(options))
.use(function (req, res) {
res.send(req.pagination);
});
pageParam
specifies the query param for the page number (defaults topage
)ippParam
specifies the query param for the number of items per page (defaults toipp
)page
the default page number (defaults to1
)ipp
the default number of items per page (defaults to24
)
Through req.pagination
the following object becomes available:
page
the current requested page numberipp
the current requested number of items per pageoptionsPresent
true
if there are pagination request parameters- Function
buildQuery(page, ipp)
returns a query string like?page=4&ipp=20
- Function
addQuery(url, page, ipp)
adds the pagination query to the url given
May not be backwards-compatible.
- Now builds the query string with the parameter names given.
- Fixed bug that resulted in wrong query strings.
- Add
optionsPresent
attribute toreq.pagination
object.
- Parse query parameters to numbers.
- The
pagination
object is now attached tores.locals
so that it can be used in express remplates