Skip to content

neoz-technologies/express-rest-paginate

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

express-rest-paginate

Express pagination middleware.

Install

npm install -S express-rest-paginate

Example with Sequelize ORM

// # app.js

const express = require('express');
const restPaginate = require('express-rest-paginate');
const app = express();

// keep this before all routes that will use pagination
app.use(restPaginate());

// with optional startup settings
app.use(restPaginate({
    offset: 0, //default: 0
    page: 1, //default: 1
    limit: 30, //default: 10
    sort: 'created_at', //default: createdAt
    descTrue: 'desc', //default: desc
    descFalse: 'asc' //default: asc
}));

//page  = limit * offset

app.get('/users', async (req, res, next) => {
  const user = await User.findAndCountAll({
    offset: req.paginate.page,
    limit: req.paginate.limit,
    order: [[req.paginate.sort, req.paginate.desc]],
  });

  return res.json(user);
});

app.listen(3000);

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 100.0%