How to configure paging method in Node Express?

Asked

Viewed 1,120 times

0

I have my paging method working perfectly with this URL:

http://localhost:3000/menuspage

See the code below;

exports.list_all_dataProviders = async (req, res) => {


    const { filter, skip, limit, sort, projection } = aqp(req.query);
    Menus
      .find(filter)
      .skip(skip)
      .limit(limit)
      .sort(sort)
      .select(projection)
      .exec(async (err, result) => {

        if (err) {
          return res.status(500).jsonp({message:"There was an internal error listing all the providers " + err});
        }
        let count = await Menus.find().count()
        res.status(200).jsonp({
          limit: limit,
          skip: skip,
          total: count,
          data: result
        });
      });
  };

This is the return in json;

{
    "total": 17,
    "data": [
        {
            "_id": "5b6abfb085dc590dc042063d",
            "id": "donut",
            "name": "Donut",
            "description": "Coberto com chantilly",
            "price": 2.5,
            "restaurantId": "bread-bakery"
        },
        {
            "_id": "5b6abfb085dc590dc042063e",
            "id": "bread",
            "name": "Pão Artesanal Italiano",
            "description": "Pão artesanal com queijos italianos",
            "price": 15.9,
            "restaurantId": "bread-bakery"
        },

>>>> e os restos dos registros.....

    ]
}

What I need is to modify my method in such a way that I can put my URL like this

http://localhost:3000/menuspage? page=0&size=4

And he returns me the first page with 4 record.

I’m having trouble modifying this method because I got it ready, and the lack of experience also contributed.

1 answer

1


Oops! you can give a search on this express link: http://expressjs.com/en/api.html#req.query

but it would be something like this:

    app.get('/:menuspage?', function mostraPaginas(req, res) {
  console.log(req.route);
  res.send('GET');
});

{ path: '/menuspage/:page?',
  stack:
   [ { handle: [Function: mostraPaginas],
       name: 'pages',
       params: undefined,
       path: undefined,
       keys: [],
       regexp: /^\/?$/i,
       method: 'get' } ],
  methods: { get: true } }

hope I’ve helped!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.