Skip to content

Commit

Permalink
pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
ThierrySans committed Oct 31, 2023
1 parent e779ea4 commit c22f6e7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
5 changes: 4 additions & 1 deletion lectures/05/src/todo/app.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,13 @@ app.get("/signout/", function (req, res, next) {
});

app.get("/api/items/", function (req, res, next) {
const limit = Math.max(5, (req.params.limit)? parseInt(req.params.limit) : 5);
const page = (req.params.page) || 0;
items
.find({})
.sort({ createdAt: -1 })
.limit(5)
.skip(page * limit)
.limit(limit)
.exec(function (err, items) {
if (err) return res.status(500).end(err);
return res.json(items.reverse());
Expand Down
15 changes: 11 additions & 4 deletions lectures/06/src/todo/app.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,17 @@ app.get('/signout/', function(req, res, next){
return res.redirect("/");
});

app.get('/api/items/', function (req, res, next) {
items.find({}).sort({createdAt:-1}).limit(5).exec(function(err, items) {
if (err) return res.status(500).end(err);
return res.json(items.reverse());
app.get("/api/items/", function (req, res, next) {
const limit = Math.max(5, (req.params.limit)? parseInt(req.params.limit) : 5);
const page = (req.params.page) || 0;
items
.find({})
.sort({ createdAt: -1 })
.skip(page * limit)
.limit(limit)
.exec(function (err, items) {
if (err) return res.status(500).end(err);
return res.json(items.reverse());
});
});

Expand Down

0 comments on commit c22f6e7

Please sign in to comment.