I would like to know in which case I would use each one for the creation of an API, I would like to know why I have an API and I want pages with it and of those elements I only use the req.body, but I don't know how the others would work, I would like to paginate and do two other things with the API for a school project.
example: draw by user or by students imagining that each student has more than one project
https:api.com/?user=juan
and in case Juan has more than 10 I only take 5 and there are 5 left for when he returns and makes the request the other 5 come out and the first ones do a findAndUpdate to another condition, revised example,
https:api.com/?user=juan&proj=5
Can you explain me the use and how to do it?
req.params
it would be to obtain information on an element of a collection catalog, for example if you had oneAPI
where/collections
it gives you the list of collections. But we want to see more information in detail an element the url would be/collections/5
where our path is internally/collections/:id
, then throughreq.params
we can obtain the value of id =>req.params.id
.While it
req.body
helps us to obtain the values that a form sends to our API. Soreq.query
it refers to thequerystring
or parameters that we pass in theurl
of theAPI
, these are the ones that you would use to do the pagination.Taking as an example
/collections
, implementing the pagination we would see something like this:/collections?page=1
o/collections?page=1&limit=20
, already from thebackend
withreq.query
we can obtain the values asreq.query.page
(for the page we want to see) ,req.query.limit
(in case we want to see more elements than the ones we show by default)Taking John's example and rearranging it:
/users/:id/projects
[here we apply pagination]/users/:id/projects/:id_proyecto
[to see the details of a particular project]