Placeholder Image

字幕列表 影片播放

  • Hey, what's up, guys?

  • It's pager here from new coder dot com.

  • And in this video, we're gonna give an overview of the crowd application that we're gonna be building as well as going over what things you should be familiar with in order to follow along with this tutorial Siris.

  • So the technologies that you should be somewhat familiar with are no Js and express Js, which we're gonna be using for our server side.

  • We're gonna be using a database cone, mongo, D B, And for our client side, we're gonna be using Jake Riri, the Fetch A P I and Bootstrapped, and you should know a little bit about HTML.

  • And last but not least for our server side and client side.

  • We're gonna be using a programming language called Java script.

  • So now let's actually take a look at the application that we're gonna be building within this tutorial.

  • Siri's.

  • So this is gonna be our to do application that we're gonna be building.

  • It's a very simple interface, so we just have a user input box where we're gonna type are to do so let's say I wanna tight clean garage.

  • I can do so it create, and you could see that this gets stored.

  • Now, if I hit Refresh, you can see that the data persist.

  • So that means that we are saving it with in our database.

  • If I want to edit this for something, let's say clean room.

  • Instead, I could click at it and you could see that we get updated with clean room it fresh data persists.

  • And if I hit the league, it deletes the two D'oh!

  • And if I want to create multiple two DUIs, I could say clean room, clean house and etcetera.

  • And all of this data will persist within Armando de be database.

  • Welcome to part one of building our crowd application from scratch.

  • So essentially, what we're gonna do in distant Oriole isn't stall our packages, set up our express application and connect to our mongo D B database.

  • So to get started, I'm just gonna go to file, Gonna go open folder.

  • I'm gonna right click, go to new.

  • We're gonna create a new folders.

  • I'm just gonna call it mongo D b underscore cried Let's select it.

  • And afterwards, when I want to do now is let's open up the terminal and from here.

  • I'm just gonna type Seal s to clear to terminal.

  • And that's because there's a visual bug with visual studio code at the moment.

  • And what I want to do now is install our packages that our application is gonna be dependent on.

  • So I'm gonna type MPM and it and we're gonna pass in the flag wife to get the default values.

  • And now I want to install the body Parson module and the body parts in module is gonna be used.

  • The parts Jason between the client side and the service side.

  • Next, we're gonna install Express.

  • Um, we're just gonna use express for routing.

  • Next, we're gonna install mongo D.

  • B.

  • And this is just gonna be the Mongol D B drivers, and this is gonna help us connect to our database.

  • And last but not least, we're gonna install the path module, and we're gonna be using the path mantra with just the serve a static HT on mount file to the user.

  • So now that we have our packages installed, I'm just gonna go up here.

  • I'm gonna hit control, be to bring up the Explorer.

  • I'm gonna go to package that Jason I'm just gonna change the main to AP IGs.

  • And this is just a personal preference I prefer at Dodge as as opposed to index dot Js.

  • So now I'm just gonna save that.

  • Let's close out of this.

  • We're going to create two files.

  • I'm gonna create my app dot Js file.

  • And our second file that we're gonna create is gonna be called d v dot Js.

  • And this is where we're gonna actually connect to Mongo D B Now from here, I want a code are apt dot Js file first.

  • So I'm just gonna click here, Control be get rid of that explorer and we could get rid of the terminal for now.

  • And what I want to do is start importing our modules that we just installed.

  • So I'm just gonna say, Concert Express.

  • It's equal to require express and let's give a space here and next.

  • What we're gonna do is bring in our body parts and module afterwards.

  • Let's create an instance of our express application.

  • So I'm just gonna say constant app is equal to express and let's call it now we're gonna tell our express application to use the body parts in module, and we're gonna be parsing Jason Data sent from the clients I to the service side using the body ports module.

  • Next, let's require our path module.

  • After that, let's bring in our database stuff.

  • So I'm just gonna say cons the D is equal to require baby.

  • And last but not least, we're gonna have one more variable here, and we're gonna call it collection, and we're gonna call it a collection to do so we're gonna have a database, and that database is gonna have a collection within it called to do, and that's gonna hold our two DUIs.

  • So now let's head over to our DVD dot Js file.

  • And now we're just gonna import arm mongo D B drivers.

  • I'm just gonna say cons mongo client is gonna be equal to require mongo D.

  • B.

  • And we're gonna require the mongo client from it.

  • We're also gonna require the object i d from the mongo D B module.

  • So I'm just gonna say cons object I d gonna be equal to require mongo d b dot object.

  • I d.

  • Next, let's give our database of names, so I'm just gonna say cons bebe name is gonna be equal to, and I'm just gonna call it cried mongo D B.

  • After that, we're gonna code are you are well, And this is basically the default location of where your mongo D B would be located on your local machine.

  • So I'm just gonna tight mongo d b local host 27 0 17 And the last configuration that we're gonna give our database is the options that we could pass in.

  • So I'm just gonna say mongo options and we're gonna be using the new U R L Par, sir.

  • Let's set that the truth.

  • So now let's actually give this a state.

  • So we're gonna be using this debate.

  • I Js file to actually create the connection between know Js and Armando D B Server.

  • So now I'm just gonna say Kant state and the default state of this is gonna be able to know.

  • So this is going to signify that Hey, we don't have a database yet.

  • Next, let's actually write our connect method, so I'm just gonna say concert connect is gonna be equal and we're gonna pass on a call back, and we're just going to say the following if state that d B So if there is a database connection, we're just gonna call to call back if there isn't a database connection, we're gonna use the mongo client to connect to the database.

  • And here we're gonna check if there's any errors.

  • If there's an error, we're gonna pass it back to our callback.

  • If there's nowhere, we're gonna set the state, and then we're gonna call our callback.

  • All right, So this is our connect method.

  • Let's not forget to add the semi colon here, and we got two more functions, the code.

  • So let's get to it.

  • So the next function that we're gonna code is to get the primary key.

  • So I'm just going to say cons get primary key and they're gonna pass in the i d.

  • Of the document, and all we're gonna do is return object.

  • I d.

  • And we're gonna pass in the i d.

  • That they passed in tow us.

  • And this is gonna return an object i d.

  • Object, which will be used to actually Cleary the database by the Primary T Next.

  • Let's actually create a method.

  • It's actually get the database.

  • So we're just gonna say, get TB, and I'm just gonna say Return state that baby.

  • So now let's actually expose all these functions that we create it.

  • So I'm just gonna say module that exports and I'm just gonna say, get the b connect and get primary key method.

  • All right, so now I'm just gonna say this and now let's head back to our app dot Js file.

  • So now that we have all this set up, let's actually connect to our database.

  • So now I'm just going to say D B Dock Connect.

  • And this is the function that we just created an R D B file.

  • We're gonna pass in a callback, and we're gonna say, if era, we're gonna consul about log unable to connect to database, and we're gonna terminate the application and we should probably wrap this within curly braces.

  • And if we were able to connect to the database successfully were going to say else after I listen and you could use whatever port that you want, I'm just gonna use Port 3000 and we're gonna pass in a callback, and we're just going to say, connected to a database.

  • AP listening on Court 3000.

  • So now, if I was to save this, that's put a semicolon here and then save it.

  • Let's go open up a terminal, seal us again in a typed note app, and you could see that we got no error.

  • So that means we bypassed this air statement and we executed our l statement.

  • So we're connected to database, and our APP is listening on port 3000.

  • Welcome to part two of building our crowd application from scratch.

  • So in this tutorial, we're gonna be covering our service side re portion of our cried application.

  • So we're gonna code to get routes.

  • The first get route is actually going to sun a static HTML files to the user, and the second get route is actually going to query the database for order to dues if an art to do collection and it's gonna return that to the user.

  • So let's actually get started.

  • Encoding this.

  • So I'm just gonna go down here, I'm gonna say after Yeah, and we're gonna give it a path of four slash give it this function, and then when we get down here, all we're gonna do is send a static HTML file, so I'm just gonna say rez dots and file.

  • And here's where our PATH module comes in handy dot Join Now, this file doesn't exactly exist yet, but we're gonna be creating this within a future tutorial.

  • So now let's actually head on over to our second at dot Get, and I'm just gonna call this app.

  • That and I'm gonna call this.

  • Get two DUIs, then here, we're gonna pass in our function, our requests object our response object, And within here, I'm going to say D b get TB.

  • Now, remember, get TB is gonna return to us our database connection.

  • So now I'm just gonna say dot collection and we're gonna pass in the name of our collection.

  • I'm gonna call them at that find, and we want all the documents within our to do collection.

  • But this is going to return to us a cursor and we don't want the cursor.

  • We want natural documents.

  • So I'm going to call and met.

  • They're called dot too, are a and then within here and this is gonna take a callback function.

  • I'm just gonna say error documents, and that's actually move this over a little bit.

  • And then within here.

  • We're just going to say if there's an error, we're just gonna consul this out to the user.

  • But traditionally would want to send an error message back to the user.

  • But if there is no error, what we're gonna do is first we're gonna print this out onto the council just to make sure that we're getting our documents back from the server.

  • And we're going to say rez dot Jason the documents.

  • All right, So now let's actually test this out.

  • So I'm just gonna hit control has to save.

  • Let's bring up the terminal or a new terminal.

  • I'm gonna type C E o s node app.

  • Let's bring up Google Chrome type local hosts Port 3000 and we called it Get two DUIs and to see that we get back an empty array, and that's because we don't have anything within our database.

  • So let's head back to visuals to be a code gonna hit control.

  • C Cancel out of that.

  • Let's start out the Mongo show.

  • And now I'm just gonna go back up here, Control B.

  • Let's find out what we named our database.

  • So we named it crowd mongo D.

  • B so I'm just gonna say use cried mongo d b I'm gonna say d b dot And let's go back to app that j s and see what we named our collection to do I'm just gonna say to do dot insert and we're gonna insert a couple of to do so I'm just gonna say to do and I'm going to say clean room So let's insert that and let's insert clean I don't know Garage.

  • So let's answer that.

  • All right, so let's cancel out of that note app.

  • Let's wait for our express application to boot up and a workroom.

  • It refresh, and you can see that our two DUIs are being sent to the client side and that it is in Jason.

  • Welcome to part three of building our crowd application from scratch.

  • So in this tutorial, we're going to be covering our server side up they portion of our crowd application.

  • So to get started, let's actually covert our route.

  • So I'm just going to say at the output and I'm gonna pass in or slash colon, I d.

  • Now, this is gonna be our route program, and I d is gonna be the primary key of the document that we wish toh up the next we're gonna pass in our function.

  • So I'm just gonna say request and response now from here, let's actually get the i d.

  • So I'm just gonna say const to do i d is equal to requests dot Haram's, But I d next one I want to do is actually get the user's input.

  • And that's gonna be found in the request.

  • That body the user's gonna be sending us, Jason.

  • So I'm just gonna say concerts user input is equal to request that body.

  • Now from here, let's actually connect to our database.

  • So I'm just gonna say d v dot Get Devi.

  • Then we're gonna call our collection, which is our to do collection.

  • Now, from here, I'm gonna call a function called find one and up there, And the first argument is gonna be too Crary object.

  • So what do we want to find by?

  • So we're gonna find by the i d.

  • So I'm just gonna pass a night the and I'm just going to pass in to do next.

  • What we want to do is pass in the document that we want to update with.

  • So I'm just gonna say Set and I'm going to say to do colon user input Got to do next.

  • What I want to do is pass in an option, and the option that I want is I want the return original Toby set the fall.

  • So I'm just gonna say, return original and we're going to set that the false.

  • Let me just move this here.

  • Now, from here, we're gonna pass in our callback function, so I just want to say error and we're gonna get the result.

  • So now let's scroll down here and from here, we could test if there was any errors, and I'm just gonna print it out onto the Consul.

  • But traditionally would want to send something back to the user to let the user know that Hey, when we try toe update the document that you want it, we couldn't fulfill that request.

  • So now I'm just gonna say else so if everything went well, we're gonna be sending that data back to the user and Jason format, so I'm just gonna say result.

  • Now, let's add a semi colon here, and let's just double check this and all this looks good.

  • So let's actually test this out.

  • So I'm gonna open up the terminal, clear a terminal node app.

  • Let's start up our application.

  • We're going to go to post men, and you can see here that I already have this set up.

  • So the euro is local host Port 3000.

  • This is the I d of one of documents that I have currently we're making a put request.

  • We're sending back Jason data here, so you could see here.

  • And this is the data that we're sending back.

  • And we can make this whatever you want.

  • So let's just change this, too.

  • Uh, clean something for tutorial.

  • All right, so this is gonna be our to do, and I'm gonna go here and make a request, so I'm just gonna hit send, and you can see that we get an error and right away I already know what's going on.

  • So when we passed in our to do, this is a string we want an object.

  • I d object.

  • So I'm gonna call devi dot get primary key.

  • We're gonna pass in the I d.

  • Of this to do.

  • I'm going to save this.

  • Let's actually cancel this note after rerun our application, we're gonna go to postman again.

  • And this time let's actually go over the response we got.

  • So it said the number that we found was equal to zero.

  • And that's because we didn't pass the right I d for the primary key.

  • So now if I send this, this number should be one, and updated existing should be equal to true.

  • So let's end it.

  • And you could see right here the number that we've updated is one.

  • This document did indeed exist within our database.

  • So it's true.

  • And you could see the values that we have the ideas, though the same.

  • But we've updated it to clean something for a tutorial.

  • And if we hit another thing, let's actually make sure that this update is working and we're gonna say change to something else.

  • I'm gonna hit send one more time and you could see the idea is the exact same thing.

  • But we've changed it to do to change to something else.

  • Welcome to part four of building our credit application from scratch.

  • So in this tutorial, we're gonna be covering our service.

  • I create portion of our crowd application so to get started.

  • We're just gonna code our route.

  • So I'm just gonna say app about Post.

  • So we're gonna be posting that to do that, we want to insert within our database, so we're gonna give it the path of forward slash and then we're just gonna pass in our function that request object and the response object.

  • Now, from here, when I want to end up doing is getting the uses input, though, To do that, the user wants to answer inside the database.

  • So I'm just gonna say, const.

  • User input and we're just gonna say request that body So the client side they use is gonna post Jason to us, and that's gonna be within the request.

  • That body next, let's actually get the database connection.

  • So I'm just gonna say d b dot get bebe.

  • And from here, I'm just gonna say collection, fasten our to do collection.

  • Then I'm gonna call the function insert one.

  • The first argument is gonna be the document that we want to insert, so I'm just gonna say user input and the second argument is gonna be the callback function.

  • So I'm just gonna say error and I'm gonna say result.

  • Now from here, we're gonna test that there's any errors.

  • So I'm just gonna logging onto the consul now, in a production environment, you would want to say something more than just logging it onto the Consul.

  • You'll probably want to display an error message to the user.

  • Next.

  • I'm just going to say, Oh, wth we're just gonna send it back to the user.

  • I'm just gonna pass it as Jason.

  • I'm gonna say result and I'm gonna say document result that ops Index zero Now, we're actually gonna test this out.

  • So you actually see what's inside is Oh, and what's inside?

  • Result got ops.

  • So now I'm just gonna save this.

  • Let's open up the terminal new terminal.

  • It's gonna clear it.

  • Gonna say node app.

  • Lets start up server.

  • Let's head over to postman and to see that we have our local whole sport 3000 here with the Ford slash you, Earl, we're making a post request and this is a Jason object that Rhys ending back to the server.

  • So let's say that we want a post.

  • Ah, clean something.

  • So this is a to do that we want to insert within our to do collection.

  • So now I'm just gonna hit send.

  • And now you can see the response that we're getting back from the server.

  • So this is the result that we sent back.

  • And this is the document.

  • So result has a property of end.

  • So that's the number inserted, and it says that it was okay, So it was set to one.

  • So that means everything was inserted properly.

  • And number two is the document that we just inserted so you could see that to do here, matches up here, and it's given an primary key which is listed here.

  • So now let's actually see if this was saved.

  • So I'm gonna go back here.

  • Let's make a get requests.

  • Remember what was our route?

  • We said, get two DUIs.

  • I'm gonna go to send, and you can see right here that are to do clean.

  • Something was inserted inside the database properly.

  • Welcome to part five of building our credit application from scratch.

  • So in this tutorial, we're going to be covering our server side delete portion of our crowd application.

  • So to get started, let's actually code are out.

  • So I'm just gonna say app not delete.

  • And the path that we're gonna use is an express Paramus.

  • So I'm just gonna say, forward slash colon i d.

  • And this idea is gonna be di di, the primary key of the to do document that we want to delete from our database.

  • Next, we're gonna pass in our function our request object, our response Object.

  • Now we've been here.

  • The first thing we need to do is get the i.

  • D.

  • So I'm just gonna say constant to do I d is equal to request that Haram's But I'd be next.

  • Let's actually connect to our database.

  • So I'm just gonna come down here.

  • I'm going to say d v dot Get bebe.

  • Then I'm gonna call the collection method.

  • I'm gonna pass in our collection or to do collection.

  • Then I'm gonna call the function, find one and believe so The first argument is the career e object.

  • So we want to find this document by the i d.

  • So I'm just gonna say I d that I'm going to say, d b dot get primary key and we're gonna pass in our to do I d.

  • The second argument is gonna be the callback function.

  • So we're going to get an error or we're gonna get the result.

  • Let me just fix this by adding a curly braces here.

  • Error or result.

  • Let's see what's happening here.

  • And this looks good.

  • So we have our error and results next, we need to test to see whether or not there was an error.

  • So if there was an error, that means we couldn't delete any.

  • So if there was an error, what we want to do is actually print the sound to the consul.

  • You can also send some kind of warning back to the user that an error has occurred.

  • Otherwise, where we're gonna do a son, Jason, Data back to the user.

  • So now I'm just gonna save this.

  • I'm gonna go to terminal you terminal.

  • It's clear to south node app and our application is running.

  • If I go to post men, you could see that I already have this running.

  • So we have our get to do is we're out here, so I'm just gonna send this again.

  • So you guys see that these are order to do's within Armando de be database.

  • So what I need is let's say I want to delete this to do inserting into the database.

  • So I'm just gonna copy this primary key.

  • We're gonna go back up here.

  • That's paste it.

  • So we have local host Port 3000 and the I.

  • D of the document that we want to delete.

  • I'm gonna go here.

  • We're gonna make a delete request.

  • And now, if I hit, send, Let's see what we get back.

  • And this is the response that we're getting back from the server.

  • So number deleted is one.

  • And you could see the value of the document that we delete it, and we get okay with the value of one, So everything went fine.

  • So let's actually proved this out.

  • If I go back here, get are two DUIs.

  • One more time, it, son, you can see that the document that was there is no longer there.

  • So this is pretty much the service.

  • I delete portion of our crowd application woken apart, six of building our crowd application from scratch.

  • So on this tutorial, we're gonna be building our static html file that we're gonna be serving to our user.

  • So in the previous tutorials we've created to get Route one of them is to serve the static HTML fall to the user.

  • And the second get route is to actually get auto two DUIs within our database.

  • But we're gonna be focusing on this route.

  • So to get started, I'm just gonna hit control, Be to bring up the Explorer, Gonna right click new file.

  • And we're gonna create a new file called Index that HTM.

  • Now I'm now I'm just gonna hit control.

  • Be again, Get rid of that.

  • And now I just want to head over to get bootstrap dot com So we're gonna be using bootstrap for in order to build our HD on now file.

  • So from here, you can see that bootstrap actually gives us a starter template, and this is going to include all the CSS that we need and the Java script that we need.

  • So I'm just gonna copy this.

  • Let's head back over to visual studio code.

  • And now I'm just gonna paste this in here.

  • Let's actually change the title.

  • So I'm just gonna change it to do bride application, and we're going to get rid of our H one tag here.

  • So from here, let's give it a container the first row is gonna be where we input our to do user input.

  • The second row is gonna be an orderto lists, which is gonna be displaying our two dues to the user.

  • Now, from here, let's head over back to bootstrap, and we're gonna get our form.

  • But we're just gonna head over to the search bar.

  • I'm gonna type form group.

  • I'm just gonna click that, and this is gonna take us here, and this looks pretty good.

  • So we have our label and we have an input where the user can type his to deuce to submit.

  • I'm just gonna copy this.

  • Let's head back over to visual studio code and we're just gonna pace this within here.

  • Well, let's actually tidy this up a bit.

  • Now.

  • We only need one of these, so I'm just gonna delete the 2nd 1 Now, let's just scroll over to the right a bit and we're gonna change some of this stuff, But we're gonna change the example able to to do, and we're gonna give an I d.

  • Attributes to our input field.

  • But we're just gonna change this from form group example and put two to do user input, and we're gonna change the place holder to say to do next, we actually have to create a button so that the user can actually submit, and we're gonna give it a class of primary, and that's actually called a button create.

  • Now, you could call this but in Post, because that's the end point that we're gonna be hitting when we click this button.

  • But I'm just gonna call it create, and that's gonna create our to do.

  • And before I forget, let's actually change with the labels for So it's not formed group example.

  • Input is gonna be for our to do.

  • All right, now, from here, this actually looks good.

  • So let's head over back to bootstrap and get our own ordered lists.

  • And we're gonna be using to display our two DUIs through the user.

  • So from here, let's go back to our search.

  • I'm gonna type unknown, ordered, and let's click the example they give us.

  • This is an A Nordic list, and you could see that has a bunch of list items here.

  • I'm just gonna copy this.

  • Let's head back over to visual studio code from here.

  • Let's just move over here, and we're gonna paste it in the second room, so I'm just gonna tidy this up a bit.

  • Now, let's give an idea tribute to our list group.

  • I'm just gonna say I d and I'm just gonna call it display because this is just gonna be displaying our two dues to the user.

  • And for right now, I'm gonna leave these lists items here, but we are gonna be dynamically adding these list items to our own ordered list.

  • All right, so that's it for our HTML part.

  • Let's actually start coatings on Java script.

  • So I'm just gonna come down here and we're gonna add a script tag here next, less through a document that ready.

  • So now let's actually get our html elements that we attached idea attributes, too.

  • So I'm just gonna say, Constance play and this is gonna be the display for our two DUIs.

  • I'm gonna say const form and this is gonna be the idee of our form.

  • And I'm going to say const to do user input, and this is gonna be that to do that the user types in Well, first off, I think I forgot to add the I D to deform.

  • So let me do that now.

  • Right here we have our form.

  • We're gonna give it an I d.

  • Attributes and we're gonna call it form.

  • And we're giving this idea actually be to reform because we want to prevent default.

  • So when the user actually submit, we're gonna prevent the default action that takes place because we wanna make a rest ful call to our server a p I.

  • Now, let's go back down here.

  • So next we're gonna coat several helper functions, so I'm just going to say, constant reset two DUIs and put and this is going to reset that to do user input field.

  • Next, we're gonna create a function called build ideas, and this is gonna build unique i d.

  • S to give to our HTML element.

  • And it's gonna take in a to do document and we're just gonna return an object with our i d.

  • So I'm just gonna say, edit I D and edit idea is gonna be attached to our edit button so we can attach a click event to that edit button, and you could see that we're taking advantage of the primary key that we get from our to do document to make it unique.

  • Next, we're gonna code dally I D and delete idea is gonna be attached to our delete buttons that we could attach a click event to our delete Bunin.

  • Next, we're gonna create a list item i d.

  • And this is gonna be the idea of the ally element within our own ordered list.

  • And the last idea that we're gonna create is gonna be called to do I d And this is gonna be the i d of our to do.

  • Well, now let's code another helper function, and this function is actually gonna build the template.

  • A list item that we want a pento are on ordered Lis, I'm just gonna say, constant build template and it's gonna take in two parameters.

  • I'm gonna pass in our to do and the I d.

  • S.

  • And we're just gonna return a string that will be a pendant.

  • So from here, let's actually screw up and let's get our ally element.

  • So I'm just gonna copy one of these and realistically, we could just delete this.

  • Well, now let's go back down.

  • Let's pace this one within here.

  • Let's delete this Now let's give this ally element and I d So we're just gonna say I d it's gonna be ableto ideas thought list item i d.

  • Next, let's give it a rule, and we're gonna make each columns of size for Well, the first column is where we're actually gonna be storing our to do.

  • I'm just gonna give it an attribute of i d.

  • And we're just going to say I d s thought to do I d And we don't want to actually display our to do to the user.

  • I'm just going to say to do dot to do next.

  • Let's go back to the last column.

  • I'm just gonna give this an additional glass, and I'm just gonna costs a text to the right.

  • So we want our buttons to be to the right of this, and we're gonna have two buttons with in here.

  • So I'm just gonna say, but in hi the courts of button, we're going to give it a class of secondary, and we're gonna give it an attributes of the i D that we create it, and the button is just going to say now that we have our edit button let's copy this.

  • And now it's time to make order, delete button.

  • But we're gonna change this from secondary to the danger class.

  • And we're gonna change ideas, too.

  • Billy, I d And the name of our Bonin is gonna be called believe.

  • Well, let me just take a look at this to make sure I typed out everything all right?

  • And this looks good.

  • And the last helper function there were gonna be coding is called display to do's and that's actually gonna display are two dues to the user.

  • I'm just gonna say, constant display it is, and we're gonna pass in data, and this data is gonna be an array of two DUIs.

  • I'm just gonna say data dot for yeah, and we're gonna get it to do out of each of these Next.

  • Let's get the i.

  • D.

  • S of each to do so.

  • I'm just gonna say let I d s equal killed I d.

  • S.

  • I'm gonna pass in our two d'oh!

  • Next.

  • Let's actually upend each of these list items to our own, ordered a list, So I'm just gonna say, display a pen and we're gonna call the build template method that we just created and we're gonna pass in our to do and our i.

  • D.

  • S.

  • Afterwards, we're gonna call our edit to do function.

  • Now, we haven't created this function yet and will do so in the next tutorials.

  • But basically, the edit to do function and our delete to do function is gonna add a click event to our edit button and our delete button.

  • I'm just gonna say to d'oh!

  • And we're gonna pass in ideas thought do I d and ideas thought at it I d and likewise brings gonna say delete to d'oh!

  • And we're gonna pass in our to do All right, These thought list item I d and ideas thought, Billy, I d.

  • So let's add a semi calling here.

  • And this is pretty much where I want to leave you guys that So we created our HT, m o phile, and we've added all our helper functions.

  • That's gonna help display our data to the user broken apart.

  • Seven of building our crowd application from scratch.

  • So, in this tutorial, what we're gonna do is display are two dues to the user By making an http get requests using the fetch a p I.

  • So if you remember from our previous tutorials we caught it does get route this and get two DUIs.

  • And the job of our get two DUIs route is to actually get all our two DUIs from the database and return it, and it's gonna be returned within an array.

  • Well, now, if I head over to our index dot html, what we're gonna do is have the fetch A p I call this get to do throughout.

  • I'm just gonna go here, and I'm gonna create a function called Get to Do.

  • So I'm just gonna save cons yet two DUIs And within here we could use the fetch a p I.

  • So I'm just gonna say fetch and it's gonna be the euro or the end point that we want to hit.

  • So I'm just going to say, get two DUIs.

  • The next argument is gonna be the type of method that we want to use.

  • So we called it a get method next week accorded then method, and we're gonna get a response back from the server.

  • And I'm just gonna say, return response that Jason and this is gonna parse the Jason for us but this is going to return a promise when you return a promise we could chain that promise.

  • I'm just gonna call it, then met there Once again s in our data.

  • And now here.

  • I'm just gonna consul about lock the data, and we're gonna pass this data.

  • This array of two DUIs to our helper met that.

  • So I'm just gonna call display two DUIs and we're gonna pass in our data.

  • Now, that's just at this semicolon here, and let's actually invoke our get to do smart then.

  • And now, if I was to save this and let's open up a terminal and I'm just gonna clear it on that saying no act, let's start up the server.

  • Now let's open up our browser.

  • So now from here, let's just type of local host support 3000 and you could see that our two DUIs are being loaded.

  • So let's bring up the developer tools.

  • I'm just gonna hit F 12 and we go to our consul.

  • You could see the array that the servers sending us.

  • So if we open that up, you could see our produce.

  • We have our primary key end to do now let's head over back to visual studio code to see how this is working.

  • So now if we come back here, we can see that our array of two DUIs is being passed into our display.

  • Two DUIs.

  • Well, let's take a look at what display to do Is is doing so.

  • Display to Deuce is actually taking an array of two DUIs.

  • It's building its unique I.

  • D.

  • S or R html elements, and it's calling Bill template.

  • Now, Bill template is gonna be used to build our list element that we're gonna add to the UN ordered list.

  • So you could see that happening here.

  • Once we build this HTML template, we're returning it, and it is going to be appended to our unknown ordered lis.

  • But that's pretty much how we are displaying our two dues to the user.

  • Mokena, part eight of building our crowd application from scratch.

  • On this tutorial, we're gonna be making http post requests using the fetch a p I.

  • So if you remember from our previous tutorial, recode it this post route and we just gave it forward.

  • Slash is gonna take data from the users and we're gonna have the post data back via the body and you could see that returns to things the result whether or not we successfully deleted the item or not.

  • And it returns back the inserted document that we just inserted.

  • So let's head over to index dot html and let's actually code this.

  • So from here, we're gonna use our form that we have up here.

  • This is our form, and what we want to do is prevent the default form submission.

  • So we're gonna come down here and I'm just gonna say form, not submit, and that's going to take in I function and it's gonna have an event.

  • And what we're gonna do is say, ee dot prevent evil from occurring.

  • Next, we're gonna call to fetch a P I.

  • So I'm just gonna say, fetch, we're gonna use our end point of forward slash The next argument is gonna be our options.

  • So the first option is we're posting data back to deserve er, I'm just gonna say, post a second property is gonna be the body what we're sending back to the database, so I'm just going to say Jason, not string.

  • If I and we're gonna son back our to do.

  • So we're just gonna use to do user input, and we're gonna get the value that our input field has at the moment.

  • Now, let me just move this over here.

  • And the last option is we're gonna have to pass in headers.

  • And what a header is is it tells the server what kind of content you're sending back to it.

  • I'm just gonna say content, I've and we're sending back Jason.

  • Now, from here, let's call, But then method.

  • So I'm just gonna say, Then we're gonna get our response back from the server.

  • We're not return that response that Jason, which is gonna be a promise that parses the data for us.

  • We're just gonna call then on to promise that's being returned, and we're gonna get our data back next.

  • We're gonna have to test to see whether or not we successfully inserted document.

  • I'm just gonna say if the data result okay is equal to one and data dot result and is equal to one that means our to do was successfully inserted into our database.

  • I'm just gonna say let I d s equal to build i d.

  • S.

  • And we're gonna pass in our document.

  • Next, we're gonna call the display method.

  • So this is gonna upend a new list who are a Nordic list.

  • And last but not least, we're gonna have to attach our clique events to our edit button.

  • So I'm just gonna call at it to do, which is a function we haven't created yet.

  • And we're gonna pass in our two d'oh our to do I d and I d of our button.

  • Then we're gonna have to attach a vent to our delete button as well.

  • We're gonna pass in our to do the list item I d.

  • And the reason we're passing in the list item ideas because we need the i d of the list that we want to delete, and we're gonna pass in the i d of the delete button.

  • So for now, I'm just gonna comment thes two out because they don't exist at the moment.

  • Right now, we have displayed up pen, and this looks good to me.

  • And last but not least, once this successfully happens, what we want to do is actually reset our two DUIs input.

  • So let's actually test this out.

  • So I'm just gonna hit control Shift back, tick.

  • Let's clear the terminal node app.

  • Let's open up a browser.

  • Now that's actually test this out.

  • So I'm just gonna come up here and s thing post.

  • I'm going to click the create but him and to see that our to do was successfully posted to the database.

  • So I'm just gonna hit refresh to see if it actually worked.

  • And you could see that our data is persisting.

  • So that's the basics of using the fetch a p I to post to deserve, er welcome to part nine of building our crowd application from scratch.

  • So in this tutorial, we're going to be making a http delete request using the fetch a p i on the previous tutorials, you could see that we caught it, are about the league route and is using a rope parameter.

  • We get that i d of the to do that we want to delete here.

  • And then here we call the fine one and delete function he pass in our primary key or Di Di.

  • And what we end up getting is the result.

  • Whether or not we successfully deleted that to do document within our collection.

  • Now that's actually head over its index dot html.

  • And let's actually code are the leak to do function?

  • Well, now, from here, I'm just gonna say cons believe to do and it's gonna take in three parameters, Is gonna take a to do, is going to take the list item I d.

  • And it's gonna take the delete i d.

  • Now to do is the document that we want to believe list item i d is the idea of the list element within our own ordered list that we want to delete and the delete ideas the i d that we've given to our delete.

  • But now let's actually code this out.

  • First, we need to get our delete button.

  • So I'm just gonna say, the left elite, but in is equal to this next Let's just like at a click event to our delete button.

  • So I'm just gonna come down here late but in dot click and we're gonna pass in our but Candler and within here.

  • Let's actually call our delete route using to fetch a P I.

  • So I'm just gonna say fetch and we're gonna pass in our route next.

  • Let's actually passing our options.

  • So we're actually making a delete request.

  • So I'm just gonna say method and we're gonna pass in the leaf.

  • Now, from here, let's call the done method to actually execute this fact function.

  • I'm gonna say, then what we're gonna end up getting back is the response from the server.

  • Next, we're gonna return that response, and we're gonna call the Jason Method on it, and this is going to return to us a promise.

  • So now we could promise chain.

  • I'm just gonna call, then again, And we should get back our data that is fully parsed.

  • Now, from here, let's actually test to see whether or not we successfully deleted the to do.

  • I'm just gonna say, if data dot okay is equal to one, that means we successfully deleted, are to do so.

  • Now we need to remove the ally element from our own ordered list.

  • But how do we do that?

  • Well, we have our list, i d, and we're just gonna call the method remove, though this is going to remove our ally element from our UN ordered list.

  • Let's actually at a summit cool in here.

  • All right, now, from here Let's go down to our display.

  • Two DUIs.

  • Um all right, so we already have the r delete to do uncommon it.

  • So this should work.

  • Now, I'm just gonna say this.

  • Let's go back to at that.

  • Yes, but definitely open up our terminal.

  • And from here, let's actually run our application.

  • No, dap, and you could see it.

  • Our application is up and running.

  • And now let's head over to our web browser.

  • And let's actually test this out.

  • Go from here.

  • You can see our to do application is up and running.

  • I'm just gonna refresh this.

  • Just the triple check.

  • And what we want to do is actually our delete put in the sea of their work.

  • So I'm just gonna hit the lead.

  • And you see that testing post is gone.

  • If I hit delete again, you could see clean room is gone.

  • And if I hit Refresh.

  • You can see that this does persist onto the database and that those two DUIs are actually deleted from our database.

  • Welcome apart.

  • Turn of building our crowd application from scratch.

  • So in this tutorial, we're gonna be making a http put requests using the fetch a p i o In the previous tutorial, we called it our app that put route.

  • So this is the end point that we're gonna hit.

  • We're using route programs, and this is gonna be the I.

  • D of the document that we want to edit.

  • We're also sending data back via the body, as you can see here.

  • And this is gonna be the data that we want to update with right here.

  • You can see that we're finding by the I.

  • D.

  • So this gets the document that come on, update this is updating the document here.

  • And if you scroll here, you could see that we're sending back the result whether or not we were successfully ableto update the document or not.

  • So now let's actually head over to our index that html file and actually code this.

  • I'm just gonna come down here and I'm gonna call the function at it to do, and it's gonna take in three parameters.

  • First is gonna be the two d'oh beckoned is gonna be the two, do I.

  • D.

  • And third is gonna be the edit idee.

  • The first parameter to do is obviously the document that we want to edit to do I d is the idea that to do, and at it i d is the i d of the button.

  • Now, let's just come down here.

  • And now from here, when I want to do is actually get her button.

  • And once we get our boat in, what we want to do is attach a click event to it.

  • So I'm just going to say edit, but in But click s n r call back.

  • And from here we can use the fetch a p I to hit our end point in our server.

  • I'm just gonna say fetch and the end point is for its slash and the idea of the to do that we want to edit the second argument is gonna be our options.

  • So we're just gonna say, Met that and we're gonna set that So a put request Next, we're gonna set our headers, and our headers are gonna be set, and they're gonna tow our server that we're sending back.

  • Jason.

  • All right, now, from here, let's set up one more property and it's gonna be our body.

  • So what we're gonna do is actually send what we want to update our document with I'm just gonna say Jason Brock String.

  • If I this is gonna come for our JavaScript object into Jason for us and I'm just gonna say to do and we're going to get the users and put Well, now that we have our options set up, let's called it done method to execute this fetch request here, we're going to get our response from the server.

  • Now, we're gonna return that response, and we're gonna call a function called Jason.

  • Not this response that Jason is going to return a promise.

  • So I'm just gonna go down here and I'm gonna call it then and it's gonna give us our parsed data back.

  • Now, I'm just gonna test to see whether or not we successfully updated our to do.

  • I'm just going to say if data that okay is equal to one, that means that everything went okay.

  • I'm now what I need to do is actually get our to do element.

  • So I'm just gonna say, let do index equal equal are to do I d.

  • Next.

  • When I want to do is actually set our data.

  • So I'm just gonna say to do index html is gonna be set to the data that we just got back from the server.

  • Now, when I want to do is reset the user's input.

  • So I'm just gonna call the reset to do input function.

  • Well, now, I'm just gonna scroll down just to check, to see if we have are at it to do is being called and is being called here.

  • All right, so let's actually test this out and our web browser to see if it actually works.

  • I'm just gonna say this.

  • I'm gonna open up the terminal and a clear it node app.

  • And now let's head over to our browser from here.

  • You could see that we have our application running.

  • Just gonna hit refresh just to make sure.

  • And right here I'm just gonna tight whatever.

  • And now I'm gonna add it, and you could see that our to do document was updated.

  • So is gonna hit refresh again.

  • Let's see if it persists to the database and you could see that it is persisting.

  • And let's try one more just to make sure and you could see that it is indeed working.

  • Woken apart.

  • 11 of building our crowd application from scratch.

  • So in this

Hey, what's up, guys?

字幕與單字

單字即點即查 點擊單字可以查詢單字解釋

A2 初級

MongoDB教程--使用Node.js從頭開始做CRUD應用。 (MongoDB Tutorial - CRUD app from scratch using Node.js)

  • 8 0
    林宜悉 發佈於 2021 年 01 月 14 日
影片單字