-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
104 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
const express = require("express"); | ||
const client = require("../mongodb"); | ||
const router = express.Router(); | ||
|
||
router.get("/", async (req, res) => { | ||
const page = parseInt(req.query.page) || 1; | ||
const limit = parseInt(req.query.limit) || 10; | ||
const db = client.db("courses"); | ||
const collection = db.collection("courses"); | ||
|
||
const startIndex = (page - 1) * limit; | ||
const endIndex = page * limit; | ||
|
||
const results = {}; | ||
|
||
if (endIndex < (await collection.countDocuments())) { | ||
results.next = { | ||
page: page + 1, | ||
limit: limit, | ||
}; | ||
} | ||
|
||
if (startIndex > 0) { | ||
results.previous = { | ||
page: page - 1, | ||
limit: limit, | ||
}; | ||
} | ||
|
||
try { | ||
results.results = await collection | ||
.find() | ||
.skip(startIndex) | ||
.limit(limit) | ||
.toArray(); | ||
res.json(results); | ||
} catch (e) { | ||
res.status(500).json({ message: e.message }); | ||
} | ||
}); | ||
|
||
module.exports = router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3f027c2
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
lit-lab-project-hrl1 – ./backend
lit-lab-project-hrl1.vercel.app
lit-lab-project-hrl1-git-main-teracle.vercel.app
lit-lab-project-hrl1-teracle.vercel.app
3f027c2
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
lit-lab-project – ./frontend
lit-lab-project.vercel.app
lit-lab-project-git-main-teracle.vercel.app
lit-lab-project-teracle.vercel.app