AndwewanttouseDi e n v andwealsowanttouseNodeMonNowDottieenvyisgoingtoallowustopollingenvironmentvariablesfrom a dottyand V fileandknowedMontewillallowustorefreshourservereverytimewemakechanges.
Thenextthingweneedtodoisactuallyconnectorhave a dietdatabasesoultypeinMongooseConnect, andwewanttoputinherethestringwhichisourdatabaseconnectionandourcase.
Thisisgoingtobemongo, D B colon, backslash, backslash.
Wecouldjustsay D b dotonandwhatwewanttodoisonerror.
Wewanttologoutthatthere's anair, sowe'lljustsayherethisisgoingtotake a functionwithanairandwecancomeinhereandsayconsoledotthereandwecanpassitthatairthis'lljustallowustoseeifthere's a problemconnectingtoourdatabase.
Also, whatwewanttodoisitOnceweconnectsowell, saydata D b dotonce, whichmeansit's onlyoneruns.
Butonethingthatyou'llnoticeimmediatelyisthatwehaveourmongo D B databasestringinsideofourapplication, andwhenwedeployourapplication, we'regoingtowanttousesomethingthat's notourlocalhost, soweneedtopullthisoutintoanenvironmentvariable.
Solet's removethisrighthere.
GointoourdotndWecouldjustcreate a variable, whichwe'regoingtocalldatabaseyou, Earl.
Butwehavenoroutesactuallyconfiguredyet, sinceitisgoingtobe a rest, Phillipe, I we'regoingtouserestfulinpoints.
Andifyoudon't alreadyhave a stronggraspofwhatrestisandwhatrestwillendpointsare I have a reallyshortvideogoingoverarrestindepth, whichyoucancheckoutinthecardsandthedescriptionlikebelow.
Sonowlet's startcreatingourdifferentroutes.
We'regoingtowantsomeroutesforgettingallsubscribersweregonnawant a routeforgettingone.
We'realsogonnawant a routeforcreatingone.
We'regonnawant a routeforupdatingthesingleonehopesupdatingone.
Andnormally, totestthese, youmaygointothebrowserandopenitup, butyoucan't reallytestarrest a guyverywellin a browserbecauseyouhavenothingtointeractwith.
Wegetthat i d 12 back, changeitto 15 andweget 15 back.
Soweknowthat a routingisworkingandwe'reabletocallitdirectlyfromvisualstudiocode.
Now, withallthatoutoftheway, what's actuallycreateourmodelthatwe'regoingtouseforapplicationsowecancreate a foldercalledModels, whichisgoingtoholdourmodel, whichisgoingtobe a subscribermodel.
CallitmangaSequelsrequireandagain, Mongoose, thisisgoingtoallowustocreate a modelwhichwecanusetointeractwiththedatabasein a reallyeasyway.
Sowhatwewanttodoiswewanttocreate a schema.
We'rejustgoingtocallthisoursubscriberschemacannotspelleverytimewe'regonnasetthisequalto a newmongoosedotschemaandthisisgoingtotakeinhere a judgejavascriptobjectandtheobjectisgoingtohavekeysforallthedifferentpropertiesofoursubscriber.
Inourcase, we'regoingtohave a name, andthisisgoingtohaveanobject, isthevaluewillfoodandlaterit's goingtohave a subscribedtochannel.
Ithasanautomaticallygenerated I D aswellasthenamesubscribedtochannelandsubscribedit, whichyoucanseeit's settowhateverthecurrentdatethat I'm recording, thisis.
Andifwedon't passinthename, forexample, andwehitsendrequest, we'regoingtogetbackthaterrormessageandyou'llalsonoticewegetthat 400 setswhichsaysbadrequest, whichisperfectbecauseitwillcomebackasanerrortotheuserusingour a p I.
Let's putthatbacktohowitwas.
Sowehaveissuccessfullyworking a p I.
Nowlet's gobacktoourouthereandyoumaynoticethatalloftherestofourroutesaregoingtobetakingan i.
Sowe'rejustgoingtosaysubscriberisgoingtobeequalto a waitingthissubscriberdotfindby i d.
AndthisAllwehavetodoispassanideawhich, asyouremember, I saidthatrequestdotprogramsdot i d isgoingtobethecorrelatingtothevariablethattheypassedinsideoftheroutehere.
Sothisisgoingtotrytoget a userbasedontheideathattheypassusinsideofthegirl.
Soifthisgroupscribeerdoesnotexist, wewanttodoiswewannareturnhereandwewanttosetthestatusrightheretobeequalto 404 and a 44 statusmeansthatyoucouldnotfindsomethingandwhichisexactlywhathappenedinourcase, We'renotabletofindthisprescriber.
Andthen, ofcourse, wewanttosendback a messagetotheuserknowsthat.
What?
Sowe'llsendback a messagehere, andthismessageisjustgoingtosay, cannotfindsubscriber.
We, ofcourse, when I catchthatairandinsideofhere, allwe'regoingtodoiswewanttosaythisisgoingtobeourupdatedsubscriberlookssubscriberisgoingtobeequaltoawaitrezdotsubscriber, you'regoingtogetoursubscriber, andagainwe'regonnausethatsavefunction.
Andlet's saywewanttochangethenamesowe'regonnasetthenamehere, twonewname, andnowifweactuallysendthisrequest, you'llseethatitsaysitcannotfindthesubscriberwiththis I d Andthat's becausethisideanolongerexists.