{"id":17628,"library":"express-paginate","title":"Express Pagination Middleware","description":"express-paginate is a Node.js middleware and view helper library designed for pagination within Express applications. It provides utilities to handle `req.skip` (aliased as `req.offset`), `req.query.limit`, and `req.query.page` automatically, simplifying the process of fetching paginated results from a database and generating pagination links. The current stable version is 1.0.2, released in 2016. The package has seen no significant updates or active development since then, indicating an effectively abandoned status. It differentiates itself by providing both server-side request parsing for pagination parameters and a view helper function (`paginate.href`) for rendering pagination controls in templates, integrating directly into the Express `req` and `res.locals` objects.","status":"abandoned","version":"1.0.2","language":"javascript","source_language":"en","source_url":"git://github.com/expressjs/express-paginate","tags":["javascript"],"install":[{"cmd":"npm install express-paginate","lang":"bash","label":"npm"},{"cmd":"yarn add express-paginate","lang":"bash","label":"yarn"},{"cmd":"pnpm add express-paginate","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"This package is CommonJS-only, released before widespread ESM adoption. Use `require` for full functionality.","wrong":"import paginate from 'express-paginate';","symbol":"paginate","correct":"const paginate = require('express-paginate');"},{"note":"The `middleware` function is a method on the `paginate` object returned by `require()`. Attempting to destructure it with ESM named imports will fail.","wrong":"import { middleware } from 'express-paginate';","symbol":"paginate.middleware","correct":"app.use(paginate.middleware(10, 50));"},{"note":"The `href` function is exposed via `res.locals.paginate` after the middleware runs and is invoked with `req` first, then with specific page/param arguments.","symbol":"res.locals.paginate.href","correct":"res.locals.paginate.href(req)(true)"}],"quickstart":{"code":"const express = require('express');\nconst paginate = require('express-paginate');\nconst app = express();\n\n// Dummy data for demonstration\nconst items = Array.from({ length: 100 }, (_, i) => `Item ${i + 1}`);\n\napp.use(paginate.middleware(10, 50)); // Default limit 10, max limit 50\n\n// Optional: Limit '0' for infinite results can be a security/performance risk\n// Add a middleware to prevent it if needed.\napp.all('*', (req, res, next) => {\n  if (req.query.limit <= 10) {\n    req.query.limit = 10;\n  }\n  next();\n});\n\napp.get('/items', (req, res) => {\n  const { skip, query } = req;\n  const limit = parseInt(query.limit, 10) || 10;\n  const page = parseInt(query.page, 10) || 1;\n\n  const paginatedItems = items.slice(skip, skip + limit);\n  const itemCount = items.length;\n  const pageCount = Math.ceil(itemCount / limit);\n\n  res.json({\n    items: paginatedItems,\n    page,\n    pageCount,\n    itemCount,\n    pages: paginate.get      // Example of generating page numbers (not directly in express-paginate, usually a helper)\n    // For actual previous/next links, use res.locals.paginate.href\n    // next_page: res.locals.paginate.href(req)(false, { someFilter: 'value', limit: limit + 5 }),\n    // prev_page: res.locals.paginate.href(req)(true)\n  });\n});\n\nconst PORT = process.env.PORT || 3000;\napp.listen(PORT, () => {\n  console.log(`Server running on http://localhost:${PORT}/items`);\n  console.log('Try: http://localhost:3000/items?page=2&limit=5');\n});","lang":"javascript","description":"This quickstart initializes an Express application with `express-paginate` middleware. It demonstrates how to apply pagination to a dummy dataset, using `req.skip` and `req.query.limit` to slice results, and mentions the `res.locals.paginate.href` helper for generating navigation links. It also includes the recommended security fix for `limit=0`."},"warnings":[{"fix":"Update database queries to use `req.skip` (e.g., `Model.find().skip(req.skip)`).","message":"The `paginate` middleware sets `req.skip` instead of `req.offset` since v0.1.0, though `req.offset` is internally aliased to `req.skip` for compatibility. Code expecting `req.offset` directly should use `req.skip` or be aware of the alias.","severity":"breaking","affected_versions":">=0.1.0"},{"fix":"Implement a custom middleware to enforce a minimum or default `limit` value if `req.query.limit` is `0` or too low (e.g., `if (req.query.limit <= 10) req.query.limit = 10;`).","message":"Passing `?limit=0` to the pagination middleware will return all results. This can lead to severe performance and security issues (e.g., denial of service attacks) if not explicitly handled.","severity":"gotcha","affected_versions":">=0.2.0"},{"fix":"Consider migrating to a more actively maintained pagination library or implementing custom pagination logic, especially for new projects or applications requiring robust security and long-term support.","message":"This package is effectively abandoned, with no new releases or significant development since 2016. It may not be compatible with newer Node.js or Express versions, lacks modern features, and will not receive security updates.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Ensure your project or file uses `require('express-paginate')` for importing the library. If using a pure ESM environment, you may need a wrapper or a different library.","message":"The package uses CommonJS (`require`). Attempting to use ESM `import` statements directly will not work as expected and can lead to errors when trying to access named exports like `middleware`.","severity":"gotcha","affected_versions":">=0.1.0"}],"env_vars":null,"last_verified":"2026-04-23T00:00:00.000Z","next_check":"2026-07-22T00:00:00.000Z","problems":[{"fix":"Ensure you are using `const paginate = require('express-paginate');` and then calling `app.use(paginate.middleware(...));`.","cause":"Incorrect import statement (e.g., using `import { middleware } from 'express-paginate'` in ESM) or trying to access it before the `require` statement.","error":"TypeError: paginate.middleware is not a function"},{"fix":"Verify that `app.use(paginate.middleware(limit, maxLimit));` is called globally or on the specific routes *before* the route handler that accesses `req.skip`.","cause":"The `paginate.middleware` was not applied before attempting to access `req.skip` or `req.query.limit` in a route handler, or the middleware failed to execute.","error":"Cannot read properties of undefined (reading 'skip') (or similar for 'limit')"},{"fix":"Review middleware order and ensure only one response is sent per request-response cycle. Place `express-paginate` early in the middleware chain if it's meant to apply broadly.","cause":"While not specific to `express-paginate`, this can occur if an error or previous response sends headers before `express-paginate` attempts to modify `res.locals` or if a subsequent middleware/route sends a response after `express-paginate`'s operations were meant to be completed and another response was inadvertently sent.","error":"Error: Can't set headers after they are sent to the client."}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}