Skip to content

Node.js based forum software built for the modern web

feat: #9234, add pagination to /api/recent/posts/:term?

Barış Soner Uşaklıfffdc4e0cad9
diff --git a/src/controllers/posts.js b/src/controllers/posts.js
index 20b0ae4292..3f42f64ebd 100644
--- a/src/controllers/posts.js
+++ b/src/controllers/posts.js
@@ -30,6 +30,10 @@ postsController.redirectToPost = async function (req, res, next) {
};
postsController.getRecentPosts = async function (req, res) {
- const data = await posts.getRecentPosts(req.uid, 0, 19, req.params.term);
+ const page = parseInt(req.query.page, 10) || 1;
+ const postsPerPage = 20;
+ const start = Math.max(0, (page - 1) * postsPerPage);
+ const stop = start + postsPerPage - 1;
+ const data = await posts.getRecentPosts(req.uid, start, stop, req.params.term);
res.json(data);
};