Advertisement
shopnilSS

Pagination Route

Apr 18th, 2021
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Pagination route
  2. route.get("/pagination/pageNo/:page", paginationController ) //pagination route
  3.  
  4. //pagination controller
  5. const paginationController = async (req, res) => {
  6.     try{
  7.         const numberOfItemNeedToShow = 5; //number of item show in the page
  8.         const {page} = req.params //get data from params or url
  9.         const skipData = ((page - 1) * numberOfItemNeedToShow); //get how many items it need to skip
  10.        
  11.         const myUser = await User.find().limit(numberOfItemNeedToShow).skip(skipData)
  12.        
  13.         if(myUser){
  14.             res.json({
  15.                 myUser
  16.             })
  17.         }
  18.     }
  19.     catch(error){
  20.         console.log(error);
  21.         res.json({
  22.             error
  23.         })
  24.     }
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement