Placeholder Image

字幕列表 影片播放

  • welcome to building wet naps with the murdered stack using Mongo, D B Atlas and Google Cloud Platform.

  • We're going to be learning the murders stack by creating an exercise tracker app.

  • My name is Bo Kearns, and I am a teacher and developer with freak Oh, camp dot or GE.

  • We will be first, starting with an introduction to the modern Stack.

  • Then we'll be going over some database concepts.

  • Then we will talk about how to get started with Mongo D B Atlas, which is the database will be using for the armored stack app.

  • Then we'll be getting into our code and I will be actually creating the back end of the exercise tracker app and showing you how I do it.

  • After that, we will have an introduction to react and then start creating the front end of the exercise tracker app with react and finally will connect the front end to the back end.

  • So what is the murder stack?

  • Well, it consists of the following technologies.

  • We have manga d be expressed, react and no Js.

  • That's what Murn stands for.

  • So no Js is the JavaScript runtime environment that allows you to exercise JavaScript code outside of a browser such as On a Server and Express is a Web application framework for Know Js.

  • It allows us to easily create a Web server, and then Mongo D.

  • B is the document based open source database.

  • So in our app will be storing our data in Mongo D.

  • B.

  • And then finally, the front end is react, which is a job script front and library for building user interfaces and then, finally, at the bottom.

  • There we will be using Mongoose, which is a simple, schema based solution to model application data.

  • Basically, it makes it easier to use Mongo D B in Know Js.

  • So let's review some database concepts.

  • We're gonna just review some terminology.

  • So what?

  • A lot of people are used to our tabular or relational databases, so we think of things like databases, tables, rose and stuff like that.

  • So Mongo D.

  • B has similar concepts that use different terms, so I just want it can make everyone aware of those terms.

  • So instead of the table, we have collections instead of Rose, we have documents.

  • We can do joint operations with the look up operator instead of foreign keys, we utilize references.

  • And just as a point of clarification, a lot of people talk about mongo D B as a non relation a LL database.

  • But really all data is relational.

  • So sometimes I use the term tabular to describe the legacy databases that use tables.

  • Mongo D B is very well student for handling data with a wide variety of relationships.

  • We're not gonna cover all of these concepts in the APP today, but I just wanted to you to be aware of some of the differences.

  • So let's look at the document model.

  • This is an example of what a Mongo D B document looks like.

  • It looks a lot like Jason.

  • We can see a variety of relationships and address a title and ah mahn Cody be stores data on disk in the beasts on format, which stands for buying Mary Jason.

  • This provides a wide variety of support for data types like strings and managers.

  • The document model also allows for nesting documents inside each other.

  • These sub documents are one of the great things about the doctrine model, so it allows us to apply the concept of data that access together is stored together in an application.

  • We also have the ability to store information inside raise, which is another powerful feature of the document model.

  • And as I mentioned, these documents are Jason structured objects, which is how most developers think of things.

  • So a person is an object that has various attributes, like a job title address and stuff like that.

  • So this allows modern development practices to use the document model in a very intuitive way without having to break the data apart to put the tables and normalize things.

  • So we will be using Mongo D B Atlas, which is a really fast and easy way to get started with Mongo D.

  • B.

  • So some people would just run up Mongo D be locally on their local computer for testing.

  • But Mongo D.

  • B Atlas actually makes things a lot easier, even if you're just doing a local project.

  • So the first step for getting started with Mondo Di B.

  • Atlas is to sign in.

  • So if you go to a Web page, you'll just get this getting started screen.

  • And once you've logged into manga TV, endless, we need to set up a place to store the data we need a cluster.

  • So this is the screen that you'll see when you first get logged in.

  • And to start building the cluster, you're gonna click the new project button.

  • That's the button right here.

  • And then after you click that you'll be able to build a cluster.

  • The big re button in the middle here allows us to create an configure a cluster.

  • So once you click that, we'll bring you to this screen where you can create a new cluster.

  • So here you can see that in Mondo di B Atlas, there's a bunch of providers will be using Google Cloud Platform and then you can choose the region.

  • We're gonna be using the free tier today, which is gonna be great for our app.

  • And just when you're getting started on learning how to use things, I recommend starting with the free tier.

  • And then once you get things going, you can always switch to another tier later.

  • So you just gotta click a region associated with the free tier and then s o The free tier is just the M o sandbox.

  • Give the server side a super size of 512 megabytes, which is typically plenty to get started with, and then one thing to notice is that the bottom is always gonna show you how much things cost.

  • So in this case, it's free.

  • Then you can click, create cluster, and then the provisioning will start so you can see it says it's going to take 7 to 10 minutes to provision in, and it's often quicker than that.

  • But you know, it's just gonna take some time to step things up.

  • So once it's created, we're ready to get a couple of security things configured and finally get a connection string.

  • So once is configurable, you'll be on this screen where you conception security.

  • And just so you know, I'm doing this all in a slide show right now, but in a little bit will actually be on the actual website so you can see where all this is on the actual website.

  • But from a security standpoint, we need to set up I P White List addresses and a database user.

  • So for this project, since it's just going to be hosted on locally, we can add your current I P address for the White List I P address and Then you can create any user name and password for the Mongo D B user, so now we can get our connection information.

  • There are a few different ways that we provide information to connect to Mongo D B Atlas through the Mongol D B shell, which is a command line interface with the Application Connection String, which is what we'll be using today and through Mongo D B Compass, which is a gooey tool for interacting with data stored in Mongo D.

  • B.

  • So then, after you click, the connector application will be taken to the screen on the right here, and we're provided information on getting a connection string and then some connection examples for different languages.

  • But we'll just need the connection string, and normally you could copy here.

  • But this is just a slide show, so I can't do that.

  • But we will be going to this exact screen and copying the connection string to use in our app.

  • Okay, we're just about to write some code, but let's just quickly review the collections in our database.

  • Our applications gonna contain exercise exercises and users, and every exercise has one user.

  • Once you see how simple this is you'll realize that we could have probably just had one collection.

  • But just for the sake of example, to see how it would work, we're gonna have exercises and users collection.

  • So now we will really will get into the code.

  • So I have visual studio code up here, and the first thing we're gonna do is verify note is installed, so I'll just do node dash V and I do have it installed.

  • If you don't, you're gonna have to make sure you install it.

  • But I'll leave that up to you to figure out how to do that.

  • So next will create the initial react project by using create react app.

  • And if we use the MP ex command, then we can run, create react app without installing at first.

  • So other MPX create react.

  • Yeah, and then I'm just going to be calling it Murder Exercise tracker.

  • And then this is going to create a a directory containing the default react project template with all the dependencies installed.

  • So well, this is installing.

  • I'm actually gonna go back over here to just talk a little bit about a mongo D B object.

  • I.

  • D.

  • S So this object I d is automatically generated by the Mongol D B driver and is guaranteed to be unique across the collection so you can see different parts of the object.

  • I d represent different things, and we can define her own idea if we'd like.

  • But it must be unique for each document in a collection for this application.

  • I'm just gonna let Mongo d b handle it.

  • So let's go back over to visual studio code and you can see that our murder exercise tracker is all set up our react project.

  • So right in the terminal, we just make this a little bigger here, right in the terminal.

  • I'm going to do a CD Murn and go into the directory there.

  • And normally, if I want to start a Web server, the React Web, the react development server, we I would type and MPM start here.

  • But first we're going to create the back end, and then we're gonna come back to create the front end.

  • So we'll start by cream the back in and connecting it to Mom, Judy Bee Atlas and Ghoul Cloud.

  • And then we'll come back and write the react later.

  • So inside this folder, we're going to create new folder called Back End.

  • So a lot of people would create the back end in a separate folder from our front and folder.

  • But just to simplify things since this, since this is a simple project will just create the backend folder right in our in our, uh, react front and folder.

  • So let's go into the back and folder.

  • Okay, Now we'll create a peck is dot Jason file inside the folder by doing in P m and knit.

  • And then why?

  • To answer yes to all the questions asks.

  • Okay, now we can install a few dependencies, so MP, install express cores, Mongoose and dot E N V.

  • Okay, So what are these packages?

  • Well, we already discussed Express, which is a light and a lightweight and fast Web framework for no GS cores stands for cross origin resource sharing.

  • And this allows Ajax requests to skip the same origin policy and access resource is from remote hosts.

  • The course package provides an express middleware that can enable cores with different options, which is this is going to make it so we can easily access some something outside of our server from our server.

  • So also, we already discussed Mongoose, which makes interacting with mongo D B through node.js simpler, and the last one is no dot m v, which lows loads environment variables from a dotty envy file into process dot m b.

  • This makes development simpler.

  • So instead of studying environment variables on our development machine, they could be stored in a file.

  • We'll create a dot N V file later, so we'll install one final package globally, So I'll do MPM install G for globally and node mon.

  • So known mon makes development easier.

  • It's a tool that helps develop know Js based applications by automatically restarting the note application when files when file changes in the directory are detective.

  • So let's see.

  • Looks like we have some sort of air.

  • I wonder what a Oh I should do a pseudo whenever you're going to install something globally will do Studio install G No would mom.

  • So whenever we update our server file, it will automatically restart the server.

  • So that's what Node Mountain mon does.

  • So now it's time to create our server.

  • So, inside the back end folder, I'm going to create a new file called server dot Js.

  • So I'm gonna start.

  • I'm actually just gonna paste in some code here, and then I'll talk about what the code is.

  • So first, we're going to require all the things we're gonna need.

  • Actually, we're not going to need a body part.

  • Sir.

  • I originally thought would any body part, sir, but it's actually it's not needed in the new version of Express and this since where it's included an express.

  • So I can actually change that to express their.

  • So we're gonna We're just gonna need express and course and then this configures so we can have our environment variables in the dot M.

  • V file.

  • This is how we're going to create our express server and then you can see the port that the server will be on.

  • Then we have our middleware.

  • So this is the core's middleware, and this is going to allow us to parse Jason because our server is going to be a sending and receiving Jason and then you can see this line Here is what starts the server.

  • It starts listening on a certain port.

  • So once we have that, I can save this, and I can start the server.

  • So, Node Mon server right in the terminal here.

  • So now we have the server running.

  • You can see it's a server is running on port 5000.

  • So now we've gotten to the point where we can connect our database.

  • We can connect to our database on mongo d be Alice.

  • So at the top of this server file after this, um, require cores line.

  • I'm going to add the following line.

  • Const Mongoose equals require mongoose.

  • So Mon Goose is what's going to help us to connect to Armando de be database.

  • So now, um, the next section I'm actually just going to paste in here.

  • So after we set up our middleware, we're going to do this.

  • So we have our your eye, which is our our database.

  • You are I This is something we have to get from the Mongol D B Atlas dashboard, which will do in a minute here.

  • So here mongo stopped connect, and we pass in the u R I.

  • That's where our database is stored.

  • And that's how we start our connection.

  • So you can see we have some flags here.

  • Ah, the 1st 1 is added.

  • This use new user.

  • Partially true.

  • That's added because Mongo, the Mongol to be no Js driver, rewrote the tool it uses to parse Mongo D B connection strings.

  • And because this is such a big change, they put the new connection string Parcher behind a flag.

  • Um, and it's kind of similar for this use create index.

  • True, it's to deal with a manga d with Mangane.

  • Be deprecating the ensure index function.

  • You don't really have to remember all that.

  • Just put these things in every time to deal with some of the updates to mongo D.

  • B.

  • So for the connection string toe work, we're going to have to set this environment variable.

  • So in the server folder, first of all, I will save this, and then we're just He had air down here because we don't have this environmental variable yet.

  • So I'm going to go into my Back end folder and create a new file called dot E N V.

  • So now we need to get the your eye or the connection string from mongo D B Atlas.

  • So let me go over to that.

  • Um, here's mom, Judy Bee Atlas and I already have the um, the cluster of created.

  • So if I create quick connect here connect, you're apt to your application and I'm going to copy the connection string.

  • So now I can go back over to my file here, and we just have to create the environmental variable.

  • So remember, it's called at lists underscore U R I.

  • And then I said, What is equal to and now I'm just going to paste in the your eye connection here.

  • And one thing I have to do a TTE the beginning.

  • You'll see that there's this password here.

  • So here I just actually have to type in my password.

  • So don't tell anybody what my password is, but it's I mean 123 for extra security.

  • I always have the exact same password as my user name.

  • No, that's not true.

  • You should never do that.

  • But in this case, that's what I'm doing here.

  • So now we should If I save this and then if I restart my server, we should see mongo d be writing the Consul terminal monger D B database connection established successfully.

  • So we got that right here.

  • So connection dot once.

  • So once the connection is open.

  • It's going toe log that that mom would be database connection established successfully.

  • So we're now connected to the database.

  • But now we have to start being able to put things into the database and read things from the database.

  • So will create art database schema using Mongoose will have two entities will exercises and users.

  • So inside the back end folder, I'm going to create a new directory called Let's See What I Do Wrong There new folder called Models and Inside Models.

  • I'm going to create two new files, so we'll exercise dot model dot Js and then I'm gonna create another file called user DOT model that Js.

  • Okay, so for the user model, I'm just gonna print some code and then I'll explain what it is here.

  • So this user schema, you can see that first we require mongoose.

  • Then we get a new mongers, that schema and so all manga schema start basically the same and we have our user schema.

  • That's the name.

  • And this only has a single field, which is a user name.

  • Then we have some validations to the user name.

  • We know it's going to type string.

  • It's required we know it has to be unique.

  • Trim mean this little trim white space off the end, if someone types in some spaces, it will be trimmed off.

  • And then Max linked arm in length of three.

  • So has to be at least three characters long.

  • So then we're gonna also include time stamps, which will automatically create fields for winner was created and when it was modified.

  • And then we just have a mongo stop model user.

  • That could be anything.

  • That's just the name that we're gonna use than user schema.

  • And then what's going on?

  • We're gonna export here.

  • So a lot of this is going to look the same for any Mon goose schema.

  • Um, so I'm going to save that, and then we're going to do the exercise model so you'll see the exercise model is going to look almost exactly the same with just a few changes.

  • Zoom out just a little bit.

  • Eso Everything's the same.

  • Except this time, instead of one field.

  • We have four fields now it takes up less space because the validations are all on the same line as the field and we don't have a CZ many validations so you can see we have user name, description, duration and date.

  • So this is all the information we're gonna store about the exercises and you can see they're all required.

  • But we have some different data types.

  • We have strings, we have a number and then the date data type.

  • So now we have We've got those created.

  • And now we need to add the AP I in point routes so the server can be used to perform the cruddy applicant the crowd operations, which is create, read, update and delete.

  • So inside this back and folder, I'm gonna create another folder named Routes.

  • And inside that folder, I'm going to create a file called Exercises that Js and a file called Users That Js So we'll come back to those files first will tell the server to use the files we just created.

  • So toward the end of this file, right before this app that listen, we're going to add some lines here, so we have to require the files and then used the files.

  • So here we're requiring the files and then basically just importing them into here, and then we're doing app that use to use these files.

  • So now whenever someone goes to our route, you are l.

  • And they put slash exercises at the end, it's going toe load everything in the exercises router.

  • And if someone goes to slash users, it will load everything in the user's router.

  • Now, once I show you this on the browser, it will probably make more sense.

  • So but so you'll be seeing that soon.

  • So let's build out the router files.

  • I'll say that.

  • And then there's gonna be air here because we haven't actually built out thes router files yet.

  • Let me go to exercises That Js, um actually will start with the users that Js Okay, I'll paste in some code and then I'll explain it here.

  • So we need the express router because this is a route that we're creating, and here we're going to require the model.

  • So this is the mongoose model that we created that we just recently created.

  • And here is our first route.

  • So this is the first in point that handles incoming http.

  • Get requests on the slash users.

  • You are l path.

  • So we have our route your l, which is local host.

  • Ah, I think local host 5000.

  • So we have local host 5000 and then it's slash users.

  • And then if it's just a slash at the end slash user slash then if it's a get request, then this is going to happen.

  • So it says users not find This is a mongoose method that it's going to get a list of all the users from the Mongol D B Atlas database.

  • The fine method returned to promise, so the results are returned in Jason format.

  • You can see right here, so after it finds, then we're going to get all the users and then rest that Jason means we're going to return something in Jason format and what we're going to return are the users that we got from the database.

  • And then, if there's an air that catch, we're going to return a Status 400 with an air with just the air message here.

  • So the second and the second in point handles incoming.

  • Http Post requests.

  • You can see it has slash ad on the end, and it's a post request.

  • Then we'll handle those.

  • The new user name is part of the request body so wrecked up body dot user name is going to be is going to be assigned to this username variable.

  • And then we're going to create a new instance of user using the user name.

  • Finally, the new user is saved to the database with the save method right here, and we return.

  • So then, after the user's saved to the database, the manga TB Alice database, then we'll return.

  • User added, and Jason or else will return the error message.

  • So finally, and this is what you're to do for all Just a standard thing for these router files.

  • Monge module.

  • The exports equals router.

  • So we're just exporting the router.

  • So let me save that and I'll go into the exercises file and I'm gonna paste some code in here.

  • And these are the same two in points.

  • So it's gonna be basically the same where requiring router.

  • We're requiring our model that we created.

  • So if it's now, it's if it's the route you are, l slash exercises slash at the end to get request, then we're gonna run this mongoose command exercise that find, Then after we find all the exercises from the database will take those exercises and then return them as Jason or else there's an ear.

  • So this part is a little different, but it's it's basically the same as before.

  • If we have exercises, slash ad and it's a post request, then instead of just having a user name and the body, we're gonna have the user name, love description, the duration and the date, and we'll sign it to these variables and you can see that we're converting the duration to a number and we're converting the date to a date.

  • They had a type, and then we just create new exercise, using all of these variables that we got up here.

  • And then we save the exercise, and after it saved that, then it's a promise we're going to return.

  • The Jason exercise added, We'll add some or a P I m point soon, but first, let's test the server a p.

  • I will be using a tool called Insomnia to test the A P.

  • I.

  • Another popular tool for this purpose is postman.

  • They both do pretty much the same thing.

  • So once you get into insomnia or you could use postman will create a new post request using Jason.

  • So I'll do a post request and all this stuff could be changed on the next screen here.

  • So we have this post request and we'll go to the U R L A C T T p colon slash last local host 5000 slash users slash AB.

  • Now we should still have our server running.

  • So in the body, I will select Jason and we're going to send a user name.

  • We're gonna add a user name.

  • So I'm going to create this Jason object here, use your name boat.

  • And if I click post here Oh, I mean, I'm a click send, and it says, user added, So now we can send they get request to get a list of the users back.

  • So first, let me just add another user.

  • So I'll just do Quincy here and send that and user added, So now we can do a get request to get the list of users back.

  • So instead of users slash ad, it'll just be user slash without the ad at the end, I'll change this post to get, and we don't need this year and I'll just click send and you can see our users here with some additional information here so you can see mongo d be automatically created this object I d.

  • And then we also have the created at and updated at date here.

  • So we have the user name Bo and the used user name Quincy.

  • So we can also see the user's We just added on the Mondo di B Atlas dashboard.

  • So let me go over to that.

  • So here is the manga D B Atlas Dashboard cloud at MONTE be dot com.

  • So if I click refresh here, we should see.

  • Yep, we see our users here.

  • So if you were back on this screen, you would have to click collections, and then it's gonna load our users here and then if you see in this documents, we have Bo and Quincy.

  • So these air, the exact users that we added so we've successfully created are back end that can put items into Armando de Bee Atlas database.

  • Okay, let's add a few exercises.

  • So let me go back over here.

  • So instead of slash users, it's going be slashed exercises and those who may slash And so just so you see how this is all working, I'll go back over to our code editor, and when you go to slash ad, you exercise it slashed.

  • Add into a post request.

  • It's going to be expecting a user name, a description, a duration and the date.

  • It's going to be expecting variables in our Jason object that have all of those keys or all those names, and there's going to take all those things and is going to save an exercise.

  • So let's go back over to insomnia.

  • And in Jason, we need a user name and set that to Bo.

  • And then we need a description and we'll set that to run and then for duration.

  • And the duration is in minutes, which you'll see once we create our front end will do in nine minutes and for a date.

  • Um, I'm actually just going to copy this date from over here, Uh, once we create a front and you'll never have to actually enter a date because you'll be just selecting a date from a drop down menu.

  • But since we're entering a directly into the database with this copy and paste, so if I click send, we should see Ope cannot get because I forgot to change this to a post request.

  • Our server did not have anything.

  • Forget request to that.

  • You're all but let's try a post request.

  • Okay, exercise and ID.

  • Well, let's add one more exercise, which is going to be a bike ride.

  • Same username.

  • And this time it was for 30 minutes.

  • And I'll change this too.

  • Um, let's see, 27.

  • And if I just send that we just added another exercise so we can do a get request If I just slashed.

  • Add change this to dot Get and then click.

  • Send now.

  • We did.

  • We sent this, Jason, but it just it didn't do anything with it.

  • It's okay to send.

  • It is just extra.

  • I didn't do anything with it, but you can see over here we have our two exercises.

  • We added.

  • So your name Bo Description.

  • Run duration nine.

  • We have the date.

  • Same down here.

  • We have a bike ride, and if we go over to mongo D B atlas and then I'm just gonna click, refresh, going to exercises and you can see Here's that two exercises we added so and right from here, you can actually do some editing.

  • Like if I want to say?

  • Actually, this was, uh, 60 minutes that I can update.

  • And so we were able to easily update we can delete things.

  • You could do all sorts of changes right from this dashboard here.

  • So that makes things that's nice.

  • Kind of mixing simpler.

  • So now that we tested everything, we'll finish up the exercise routes.

  • So let me go back into the code editor.

  • Um, we're actually done with the user's routes.

  • All we're gonna have is to get an ad now in, like a real production after would have up, you'd have, ah, update and elite for now.

  • We'll just do that from the mongo D B Atlas dashboard, but for the exercises, we will have everything.

  • Well, we're gonna have all the coal crowd will have the lead update, and that's what we're gonna add.

  • Now I am going to right here.

  • I'm going to have pace than the other route, and then I'll talk about him here.

  • So we just add this round we have a slash colon I d.

  • So this Colin I d.

  • Is like a variable.

  • So this is a Mongol.

  • This is an object I d.

  • That's created automatically by Mongo D B.

  • So if you go to exercises slash and then put the object i d from the database and then you do it get request.

  • Well, we're going to return just the information about that exercise.

  • So we have exercised that find by i d.

  • And then the request that prams the i d.

  • So this is getting the idea directly from the U R l here and we're finding by the i d.

  • And then once we get the exercise, we're going to return it as Jason or else we're going to just return an heir.

  • So then the next thing is going, it starts off the same.

  • So this was the get request, and this looks exactly the same.

  • But instead of get its delete, So if you pass in the object, i d.

  • But it's a delete request.

  • Instead of a get request, it's going to do fine by I d and delete again.

  • It's going to get it right from the u R.

  • L here and it will deleted from the database.

  • So it says that then, after it's deleted, is going to return exercise deleted.

  • So the next one is the update So if their route is exercises slash update slash i d So the object i d and it's that post, Then we're going to update it.

  • So first we have to find the current exercise, and then we update it.

  • So exercise that find my i d.

  • And we passed in the parameter right from the u R l here.

  • So after we get the exercises, that then and then we what we're gonna do with the exercise is, Well, we're going to take the exercise, that user name.

  • So this is the exercise we just got from the database.

  • But we're gonna set the user name to equal wrecked that body.

  • That user name because it's going to have this route needs to receive a a Jason object that's going to create that's going to contain a user name, a description, a duration and a date, just like when you're adding a new a new exercise.

  • But this time we're taking all the information and assigning it to the fields of the exercise that already exists, and that will save the exercise with the new information.

  • And then we're going to return exercise updated.

  • So now that we've seen all these in points.

  • Let's go and test these other ones.

  • So we'll go right back over here.

  • And let's test the first in point.

  • We added So first we need to get an I.

  • D.

  • I am just going to copy this idea right from here, and then we'll have slash exercises slash i d.

  • And this is going to be a get request.

  • If I send this, it will just return a single exercise.

  • So before it returned all the exercises But now that we have this new route where it's local host 5000 slash exercises slashed, the I d.

  • Is going to return just the first exercise that corresponds to that I d.

  • The next thing we're the trial is the we're gonna try editing it, so slash exercises slash update and then So we have slashed exercises, slash update and then we have the i d.

  • Here.

  • I wonder if I can.

  • Here now will be.

  • Let's see more of it.

  • Exercises slash update slash i d.

  • And this is going to be a post.

  • This is the one where the description is run from before it has to receive a user name description, duration and date.

  • So the new description is toe run from what it was before.

  • It's going to be walk.

  • And the duration is now going to be How about 1 20 they will keep the date the same.

  • So I mean a copy this date and paste in here.

  • So right now, the way the program works is that you always have to send it the user name, description, duration and date.

  • You cannot just send the field that you wanna update.

  • You always have to send all the fields or there'll be an heir.

  • Now that's some something that you couldn't re factor later is.

  • Make it so you could just receive one of the items instead of all of them.

  • But for now, we have it set up.

  • It has to accept every single field.

  • So if I post this and then I'm going to change this into a get request and take out update, you can see that the same I d now has a description of walk in a duration of 120.

  • But actually, we don't even want that anymore.

  • So we're going to delete it instead of sitting it.

  • Get request.

  • I will send a delete and then if I send that exercise deleted.

  • So now I'm just going to go to the U R L the route that returns all of the exercises.

  • So just slash exercises slash and then a get request and you'll see that now we just have one exercise, whereas before we had to one of them been deleted.

  • So we just have the one.

  • Okay, now that we've tested this were actually done with the back end, we just had a simple back end with some crowd routes.

  • So now it's time to start the front end before we get into our front and code.

  • Let's talk a little bit about react.

  • Now, I'm just going to give a brief overview of react.

  • If you want full in depth tutorial, you can check out the reactor Torrey on the Freak o camp YouTube channel.

  • React is Ada Clara tive efficient and flexible JavaScript library for building user interfaces.

  • It lets you compose complex user interfaces from small and isolated pieces of code called components.

  • Now our project isn't too complex, but it's still gonna give you a good introduction to react.

  • We use components to tell react what we want to see on the screen.

  • So this is an example of the type of react component right here.

  • And when our data changes, react will efficiently update and rear ender are components, So this is a react component.

  • Class you can see it says, react that component.

  • That's what this extends here, and a a component takes him parameters called props short for properties.

  • And it returns a hierarchy of views to display through the render method.

  • So you can see we have a render method here, and then it's returning something here.

  • The render method returned a description of what you want to see on the screen.

  • It takes a description and displays the result.

  • It returns React element, which is a lightweight description of what surrender this Syntex.

  • Here.

  • It kind of looks like HTML, but it's actually a special Syntex called JSX, which makes the structures easier to write.

  • So this is actually converted into HTML when it goes through pre processing and Jay Access X comes with the full power of Java script.

  • You can put any Java script expressions within braces so you can see this brace.

  • These braces were, says this Stop props.

  • That name this is getting the is the proper Catholic of variable and you can see the example usage.

  • So too call this react component or to place on the page, you'll use something like we have at the bottom.

  • So we have the name of the react component, and then we pass in the props.

  • Name equals bow.

  • So right in what we're returning when it says this dot props that name, it's going to put Bo so on the page it will return shopping list for boat.

  • So another common difference between J.

  • S X and HTML isn't said class.

  • We have class name, but for the most part, you can write JSX just like you would write HTML.

  • So let's go to the code.

  • So remember earlier we did create react app that created our whole react project.

  • I'm gonna close some of this back and code because we're not gonna use that right now because we're working on the front end.

  • So right now I'm going to go over some of what, the front in code.

  • So if I go to the source directory and then we're gonna go into actually, let's start in public and we'll go to the index dot html.

  • So this is the HTML page that loads when you go to our website.

  • So let me kind of explain this.

  • You can see how react works with it.

  • So for one, we don't need any of this, These comments so delete that.

  • So it's easier to see what's going on here.

  • And you can see this line right here is where our react is going.

  • Are reacting up, is gonna load, says Divide equals route and you'll see in her Java script we're goingto load our react app right into that Div.

  • The only thing we really need to change here is the title.

  • So instead of react app, I will call this exercise tracker and save that.

  • And then if we go into our Java script that loads with her Asia Mel, it's right here.

  • So this is where a basic job script file that loads and you can see it's going toe load React, react, Tom, the index that CSS we're not writing any custom CSS so we can get rid of that importing app.

  • This is where we're actually going to create our file.

  • Are this is basically the starting point where we're gonna within the app that Js we import it here.

  • And that's where we're going to create our front and react amp and you can see down here this is the main thing that renders our aunt react on that render its going to render our app, which is what we're gonna be working on next and is going to put it here.

  • Document that get element by I d route.

  • That's what we were just looking at in the a C M L file the route here.

  • So we're loading our app into that element.

  • So we're not going to use service workers so we can delete some of this stuff when you could believe that when you use create react app, it's someone to give you some extra things that you don't need.

  • So let's go into the app dot Js That's this file here.

  • So this is our main, our main component, our main reactor app that we're gonna put all over code that will display on the page and actually let's get this started so we can see what this looks like right now I'm going to open up a new terminal window window.

  • So in this new terminal window, I'm just going to switch into their directory the x x er size tracker directory and Moran Exercise tracker.

  • And then I'm just gonna do MPM start.

  • Okay, so this is going to start our amp.

  • And it will load right here, right on local host 3000.

  • And here is our react.

  • Um, you can see it says at its source, slashed app to save and save to reload.

  • So it's going to hot reloads automatically reload this page anytime we make some changes.

  • So what I'm gonna do here is just do some basic changes here, so let's see what we're gonna dio We were not going to use this logo.

  • We're not going to use this CSS here And let's just return something basic.

  • Instead of what we have here.

  • I'm gonna delete all this and we're just going to return.

  • It's just going to say hello world and for the class they EP of this cult container.

  • So if I say this and I go back over to my browser, it says, Hello world, and so anything that we change here and up here, we'll see, I'm actually on the page so you can see this is being returned.

  • This JSX which gets loaded into our html with this job script file an appears on our page.

  • So now let's add the bootstrap CSS Framework tower project to make styling easier.

  • So I'm gonna open up a new terminal tab, and I will do in p m install.

  • Bootstrap.

  • Okay, well, that's installing.

  • I will go up here, and I am going to import Bootstrap.

  • So import bootstrap last pistes.

  • Les C s s flesh boot strapped up men dot C S s.

  • So we have bootstrapped imported now and now let's set up react router The react router.

  • So I'll do MPM and stall React router dom.

  • Okay, react router is going to make it easier to route different you Earl's two different react components.

  • You'll see how it works as we go along here.

  • So I'm going to have to import it So right at the top, right after we import react.

  • This time I'm just going to paste in a line here.

  • Import were importing browser outer as router and route from react router dom.

  • Okay, Now we can create a router element.

  • So now that we're importing this router from router React Rotterdam, we have to put everything that we want to be used with the router inside a router element.

  • So I'm gonna do here is do router.

  • And then at the end, we will put the closing router tag.

  • And then now, instead of our hello world, we're going to update it to the club to start actually building the actual app here.

  • So I'm just going to have a paste in some information here and you'll see that we have a router element for each route of the application.

  • So we have a few components these air, all components that we're still going to create.

  • We have the nab our component exercise this component at exercise, create exercise and create user component.

  • So these components we still have to create here and you'll see that there's a route element for each route in the application.

  • The path at repute is set to the U.

  • R L path.

  • So if you go to our route, you're out with a slash at the end, it will load this component if you go to the root your l slash edit and then we have an i.

  • D.

  • This is the Mongo D B object I d.

  • I'll go to the exercise component or the create component or the user the create user component, depending on which path.

  • So that's what react router does is it helps us map specific u R L paths two different components that will load on the page.

  • So we're gonna have to create those five components and we'll create them in separate files.

  • But first, let's import the files were just about to create into ap dot Js right after the import for bootstrap surrender this I will import all these with some semi colons.

  • And so you see that we're importing.

  • These are the names of the different components and these are just where they're located.

  • Components up?

  • Nah, Bardot component components like exercise list.

  • That component with these files were just about to create.

  • You can see they're all in the components directory, so that's what we'll create now.

  • So, inside the same directory as Apthe Js inside the source directory, I'm gonna create a new folder, and it's gonna be called components.

  • So inside this components folder, I'm gonna create a new file called knave Bar that component that day?

  • Yes.

  • I'm just going to paste in this code here, and then I will go over a little bit.

  • So most react components are going to start with the start.

  • The same.

  • You're always gonna import, react and component from react.

  • And then, uh, since we're using react router, I'm going to import link.

  • That's gonna allow us to link to different routes.

  • And then all components start like this.

  • Export default class nab our.

  • That's the name.

  • The component extends component, and all components have to render something.

  • So we're gonna written render this return statement.

  • And this code right here is basically just the knave bar from the bootstrap documentation converted toe work for our purposes.

  • You'll see that instead of class, we have class name, um, the links.

  • Instead of an anchor tag, we use this link from react router.

  • But it's going to show up the same way as just a link on there.

  • And we show which what we're goingto linked to here and the different you.

  • Where else you gonna link to?

  • And then we have special classes, just a style using bootstrap styles so you can always just check out the bootstrap documentation if you want to learn more about the styles you can do with that.

  • So at this point, we're going to create our other component files.

  • So I'm going to create exercises, list that component that Js I'm going to create at it.

  • Exercises that component?

  • Yes.

  • Then we'll have create exercise that camp opponent that Js then create user that component that Jess and those are all of our component files right now, we're just gonna create stubs for these components just so we can see if everything works so far.

  • So in the exercises list component Just put in this this is just some basic, just the most basic component you can have important reaction component export default, class exercise.

  • Let's component extends component, and then we're just going to render you are on the exercise list component.

  • So I'm just going to copy this code and go into the exercise component.

  • I'll pace that, and and so the exercises list we're gonna do edit exercises and again instead of you were on extras.

  • It's list component.

  • We'll just you you were on the edit exercises component, and I'll copy that again.

  • Let me make sure this is saved.

  • And here, instead of exercises, list.

  • Wait, I meant to copy.

  • Copy this.

  • It'll be easier to start with this one instead of exercise is going to create.

  • I'm just gonna select, edit and do command d to select two places at once.

  • And then I could just type in two places at once.

  • And I'll put the word Creon both spots.

  • So I'll say that and then our create user component.

  • So again, I'm actually gonna copy this code paste.

  • And here, instead of create exercises, command D I'm gonna do create users.

  • So you on the create users component.

  • The last thing I need to do is save after J s.

  • And now we should be able to test this out.

  • So let me go over to the browser.

  • Who?

  • Failing to style.

  • I see what happened.

  • This is the wrong type of quotation, Mark.

  • So there we go and let me go back over there.

  • Cannot resolved create user component.

  • Here is where I may have just, um, spelled something wrong.

  • Let's see, component and oh, yeah, I just spell this run wrong, So let me rename.

  • That's why It's good to test every once in a while while you're building things just to make sure everything's working.

  • Let's see, it's looking for exercise, and I just put it s at the end of this.

  • So stick that off and now we're gonna work and it's working.

  • So it says you are on the exercise lift component.

  • When we click the different things exercise, you are the exercise list component.

  • Click.

  • Here you are on the create exercise component and then click Here you are on the create user component and I'm back on at that Js.

  • And there was one thing I want to improve just to use the styling better from bootstrap inside this router gonna have a div and we'll set the class name to equal container.

  • And then we just need a closing div here at the end.

  • And then I'll just indent this a little bit.

  • So let's see what that looks like now.

  • Okay, I think that looks a little better.

  • So we have some space on the sides and everything's just a little more centered.

  • Okay, let's start creating our first component.

  • So we already create the stubs of the components and Now we're going to create the create exercise component.

  • So here's Cree Excise that component.

  • So this will allow us to add exercises

welcome to building wet naps with the murdered stack using Mongo, D B Atlas and Google Cloud Platform.

字幕與單字

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

B1 中級

學習MERN堆棧--完整教程(MongoDB、Express、React、Node.js)。 (Learn the MERN Stack - Full Tutorial (MongoDB, Express, React, Node.js))

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