Created
November 11, 2021 20:17
-
-
Save PavlikPolivka/68aab3e7d08a278a3c176a69ff208e3d to your computer and use it in GitHub Desktop.
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
//api/posts/[page].js | |
import {getSortedPostsData} from "../../lib/posts"; | |
export default function (req, res) { | |
const { page } = req.query | |
const allPostsData = getSortedPostsData() | |
const perPage = 9 | |
const totalPosts = allPostsData.length | |
const totalPages = totalPosts / perPage | |
const start = (page - 1) * perPage | |
let end = start + perPage | |
if (end > totalPosts) { | |
end = totalPosts | |
} | |
res.status(200).json({ | |
currentPage: page, | |
perPage: perPage, | |
totalCount: totalPosts, | |
pageCount: totalPages, | |
start: start, | |
end: end, | |
posts: allPostsData.slice(start, end) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment