@0xc14m1z/express-pagination 中文文档教程

发布于 6年前 浏览 21 项目主页 更新于 3年前

express-pagination

 TravisCI工作服代码气候 Codacy

可配置的 ExpressJS 分页中间件。


这个包的目的是简化 ExpressJS 的分页处理 应用程序。 它可以尽可能快地使用,如基本用法所示 部分,或者它可以高度配置以完美地反映您的应用程序 需要。

这个中间件允许您的应用程序读取两个参数进行分页 (当前页码和每页所需结果的数量)和设置 传递给的请求对象中的可配置属性 你的路线处理程序。

installation

npm install --save @0xc14m1z/express-pagination

default behaviour

此中间件:

  • looks for a page parameter as the current page number;
  • looks for a perPage parameter as the desired number of results per page;
  • sets a pagination property on the request object;
  • sets a page property to the given current page number or fallback to page 1;
  • sets a perPage property to the given desired number of results per page or fallback to 50;
  • sets a from property to the number of results to skip;

所有这些属性都可以按照 高级用法 部分中所示进行配置。

basic usage

这个包导出两个方便的方法用作 ExpressJS 中间件 在路线或应用程序的基础上。

如果你没有特殊需求,你可以只导入一个中间件并像这样使用它:

const pagination = require('@0xc14m1z/express-pagination')

app.get('/first-route', pagination.add, function (req, res) {
  // your handler here
})

现在你的路由可以被调用为:/first-route?page=3/first-route?page=3&perPage=30

如果未提供 perPage 参数,则使用值 50

使用此设置,您将在 req.pagination 中获得分页信息:

{
  page: 3,
  perPage: 30, // or 50, if this parameter isn't given
  from: 60
}

如果您想更改每页所需结果的默认数量,您可以 使用稍微不同的中间件:

const pagination = require('@0xc14m1z/express-pagination')

app.get('/second-route', pagination.addWith(25), function (req, res) {
  // your handler here
})

advanced usage

这个包的默认导出是一个函数,它在 输入并返回一个中间件生成器。

这个中间件生成器可以在您的应用程序中使用:

// customPagination.js
const pagination = require('@0xc14m1z/express-pagination')

module.exports = pagination(/* custom configuration here */)
// controller.js
const addPagination = require('./customPagination')

// here the number results per page is taken from the configuration
app.get('/first-route', addPagination(), function (req, res) {
  // your handler here
})

// here the given number results per page overrides the configuration value
app.get('/second-route', addPagination(100), function (req, res) {
  // your handler here
})

默认配置是:

{
  input: {
    page: 'page',
    perPage: 'perPage'
  },
  output: {
    property: 'pagination',
    page: 'page',
    perPage: 'perPage',
    from: 'from',
    defaultPerPage: 50
  }
}

假设我们有一个应用程序接收分页信息 较短的格式,例如:/paged-route?p=4&pp=30

其中 p 是当前页码,pp 是所需的结果数 每个页面,我们可以将配置设置为:

{
  input: {
    page: 'p',
    perPage: 'pp'
  }
}

假设,例如,我们正在使用 Sequelize ORM 来执行我们的数据库 查询。 它允许使用标准 SQL 关键字缩小查询结果: limitoffset

我们可以将配置设置为:

{
  input: {
    page: 'p',
    perPage: 'pp'
  },
  output: {
    property: 'queryLimits'
    perPage: 'limit',
    from: 'offset'
  }
}

并像这样使用它:

// controller.js
const addPagination = require('./customPagination')

app.get('/users', addPagination(), function (req, res) {
  const { limit, offset } = req.queryLimits

  User.findAll({ limit, offset })
  // or even User.findAll({ ...req.queryLimits })
})

express-pagination

TravisCICoverallsCodeClimateCodacy

Configurable ExpressJS pagination middleware.


The purpose of this package is to simplify handling of pagination for ExpressJS applications. It can be used as fast as possible as shown in the Basic Usage section, or it can be highly configured to perfectly reflect your application needs.

This middleware allows your application to read two parameters for pagination (the current page number and the number of desired results per page) and setup a configurable property in the request object that gets passed to your route handler.

installation

npm install --save @0xc14m1z/express-pagination

default behaviour

This middleware:

  • looks for a page parameter as the current page number;
  • looks for a perPage parameter as the desired number of results per page;
  • sets a pagination property on the request object;
  • sets a page property to the given current page number or fallback to page 1;
  • sets a perPage property to the given desired number of results per page or fallback to 50;
  • sets a from property to the number of results to skip;

All of these properties can be configured as shown in the Advanced Usage section.

basic usage

This package exports two convenient methods to be used as ExpressJS middlewares on route or application basis.

If you don't have special needs, you can just import a middleware and use it like:

const pagination = require('@0xc14m1z/express-pagination')

app.get('/first-route', pagination.add, function (req, res) {
  // your handler here
})

Now your route can be called as: /first-route?page=3 or /first-route?page=3&perPage=30.

If no perPage parameter is given, the value 50 is used for it.

With this setup you'll have pagination information in req.pagination as:

{
  page: 3,
  perPage: 30, // or 50, if this parameter isn't given
  from: 60
}

If you want to change the default number of desired results per page, you can use a slightly different middleware:

const pagination = require('@0xc14m1z/express-pagination')

app.get('/second-route', pagination.addWith(25), function (req, res) {
  // your handler here
})

advanced usage

The default export of this package is a function that takes a configuration in input and returns a middleware generator.

This middleware generator can than be used in your application:

// customPagination.js
const pagination = require('@0xc14m1z/express-pagination')

module.exports = pagination(/* custom configuration here */)
// controller.js
const addPagination = require('./customPagination')

// here the number results per page is taken from the configuration
app.get('/first-route', addPagination(), function (req, res) {
  // your handler here
})

// here the given number results per page overrides the configuration value
app.get('/second-route', addPagination(100), function (req, res) {
  // your handler here
})

The default configuration is:

{
  input: {
    page: 'page',
    perPage: 'perPage'
  },
  output: {
    property: 'pagination',
    page: 'page',
    perPage: 'perPage',
    from: 'from',
    defaultPerPage: 50
  }
}

Let's say that we have an application that receives paging information in a shorter format, for instance: /paged-route?p=4&pp=30

Where p is the current page number and pp is the desired number of results per page, we can set the configuration as:

{
  input: {
    page: 'p',
    perPage: 'pp'
  }
}

Let's say that, for instance, we are using Sequelize ORM to perform our database queries. It allows to narrow query results using the standard SQL keywords: limit and offset.

We may set the configuration to:

{
  input: {
    page: 'p',
    perPage: 'pp'
  },
  output: {
    property: 'queryLimits'
    perPage: 'limit',
    from: 'offset'
  }
}

And use it like:

// controller.js
const addPagination = require('./customPagination')

app.get('/users', addPagination(), function (req, res) {
  const { limit, offset } = req.queryLimits

  User.findAll({ limit, offset })
  // or even User.findAll({ ...req.queryLimits })
})
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文