Intoday's video, we'regonnalookatpagination, whichistheabilitytoreturnjust a subsetoftheresultsfromyour A p I totheuserssothattheycangopageforwardorPaigeback, justthesameashowGooglesearchworks.
Andwe'regonnatacklethisinthreedifferentways.
First, we'regonnabuildjustthemostbasicpaginationthatyoucanbuildinsideofan A p I.
Secondly, we'regonnatakethat a stepfurther, andwe'regonnamake a nicemiddlewherefor a paginationsowecanapplyittoanyoftheresourceisinour a p I.
Andthenthird, we'regonnahookupthissimplesectionofourpaginationto a databasethatweactuallyhave a realworldmongo D B databasethatwecanpaginatedwithour A.
P.
I wasgetstarted.
Now, asalways, I justhave a completelyblankfolderopeninvisualstudiocode, andtheveryfirstthingweneedtodoistorun N p m onitwiththedash.
Whyistheflagandthisisgoingtoinitializetherepositorieswith a packagedaJason.
Thisway, wedon't havetomanuallystopandstartourservereverytimewemakechangesnowinthepackagedotJason, inordertouseNodemon, I wasjustcreate a simplescript.
Butusuallywhenyouhave a paginated a p I, youactuallywanttoreturnsomewhereinsidethat a p I whatthenextpagewouldbeandthepreviouspagesothattheuserknowsifthereis a nextpageorifthereisn't a nextpageandsamethingwithpreviouspage.
ItjustmakesiteasierfortheclientconsumingtheAP.
I Soinordertodothis, weactuallywanttoupendextraresultstohere.
Sonowinourrequest, ifwemakethisagain, yousee, wehave a resultsectionwhichisgoingtoreturnourarrayofusersand I wecanactuallyputextrainformationinsideofourobjectforthenextandthepreviouspages.
Soit's quiteoutofourserverherewedidtoinstall a librarycalledMongoose, andthisisjustgoingtoallowustoeasilyinteractwithArmandodeBedatabasethroughthiskindofrapper.
Thatjustmakesitsomucheasier.
Nowthatwehavethatinstalled, wecanremove a lotofourboilerplatecode, what's removetheseusersandthesepostvariables, andwedon't actuallythispostgetanymore.
NowtheveryfirstthingthatyouneedtodowithMongo, D.
B andMongooseistosetup a schema, whichwillallowustoactuallyaccessMongoDeviandsaverecordsin a collection.
Sotodothat, we'rejustgonnacreate a filecalledusersdotJsisgoingtobeourschemasowecanimportMongooseintohere.
Let's say a limitofthreewewanttostartatthesecondpagewouldsendthatrequestandyoucanseewehaveourpreviousbeingshownupwithpageone, a limitofthree a resultsmeanshutupforuser's 45 andsix.