Placeholder Image

Subtitles section Play video

  • Yes, I muted myself.

  • Okay.

  • Meeting.

  • Okay.

  • Hello.

  • Welcome to a very special coating trade at this ode with a slightly different looking coding train logo Fleece, um, like plants and flowers and things will start growing from here.

  • I'm kind of vamping here to see if people chat.

  • Start to say hi that the sound is working on everything, because I, uh, straight I'm on time today.

  • And the reason is because it's not actually me who will be presenting to you very excited to introduce to you a guest CJ from the coding garden.

  • The coding garden is another YouTube channel that you should definitely subscribe to if you like coating tutorials, watching them live streamed on YouTube will put links and all that stuff in the video description.

  • I'm sure you can find it just by typing coding garden into the search bar.

  • Um, CJ is an educator, a maker, and a full stack web developer.

  • So somebody who actually knows about proper full stack web development, which is something that I definitely did not know about.

  • So I'm excited to have C J here for about an hour to do a tutorial.

  • I mean, he'll tell you all about it.

  • I don't need to try to tell you about it.

  • I'm very I'm sure I'm gonna learn a ton watching it.

  • Um, So and this will be a live stream.

  • I am going to, uh, tiptoe off to the side and, like, sit with the laptop, looking at the chat.

  • So if you have questions, ask them they're all kind of compile them.

  • We can interrupt CJ if we need to, but always let him kind of present and then ask some more questions at the end as well.

  • Did I miss anything?

  • Can you hear Seed?

  • You could hear his voice.

  • So we're gonna do the awkward shuffle.

  • Now, I don't have like, um, like a professional talk show.

  • Kind of like coding host thing where, uh, welcome.

  • And I'm gonna get my microphone so I dont backs and make too much noise.

  • Welcome, CJ.

  • Okay, thanks.

  • So, what's then?

  • Um, first off, Just huge.

  • Huge.

  • Thanks to dan and the coating train and coding train community.

  • Yes, thank you.

  • And this is gonna be a lot of fun.

  • So the plan for today is to build a full stack application, and the idea is I'm going to build a very basic Twitter clone to demonstrate all the parts and pieces of the full stack.

  • So the idea is we're going to be building me our which is Twitter for cats.

  • Andi will have two features.

  • Cats can send a mu, which is like a tweet, and cats can see all the news that they have sent.

  • So you can see a simple animation here of what we're gonna be building.

  • Basically, we have a form you put in your mu, and then when you click, send it gets sent to the server, gets put in the database than the front end requests, all of them.

  • It all comes back and you see it all.

  • And we're gonna build that today from scratch.

  • So I have all of our objectives here.

  • The 1st 1 is to diagram the full stack.

  • So we're gonna draw a picture and talk about the different parts and pieces and how all of this interacts together.

  • So let's do that.

  • So the full stack is typically split up into two pieces more pieces than that in each piece.

  • But you have the front end and the back end.

  • What is I think that's your head.

  • Okay.

  • It wasn't Cameron.

  • Wait.

  • Um, but front and back end.

  • So here, I'm going to draw the front end, and then we also have the back end.

  • And when we talk about the front end, this is what the user sees.

  • So also be referring to this as the client, the client.

  • But this is what the user sees.

  • So, uh, the user is uses a web browser.

  • So you could think of it like it's a little box here and they type in the URL and we'll have some place to type, and they'll have a button that they can press.

  • But the front end is what the user actually season.

  • This gets loaded into a browser.

  • And so you could think of this on a laptop on a desktop on a mobile phone, anything that has a Web browser that is the client.

  • And that is the front end.

  • So there's also the back end, and here we have a couple different backend server.

  • So the 1st 1 will be known as the static file server serves server server.

  • And this is where all of the files that we write to run inside of a client will live on.

  • And I guess it's important to note that there are a lot of different ways you could build a full stack application.

  • But the way I'm going to build it today kind of split things up in this way.

  • But let me know there are a lot of other ways you could do this on on the back end.

  • You also might have more of a dynamic server endemic server, and today we're right of using a node.js for that.

  • But there are a lot of other things you could use, and specifically we are going to be using Java script for this dynamic server.

  • But you probably heard of PHP or C sharp or job of those different things could be running on the server here.

  • At the end of the day, whatever is running on here will return Jason data.

  • So this is we're gonna be building a Jason A P I.

  • So if you've heard of a P I, if you've heard the word cloud or I guess, yeah, cloud, that's really when we're talking about the back.

  • When you when you say it's in the cloud, it's something back in type of server.

  • It was good.

  • Cool.

  • So we have the quiet.

  • That's the front end.

  • We have, um, the back end.

  • So we will call this the dynamic server.

  • This is where no Jess built will be running for our static file server.

  • We're gonna be using something called live Server.

  • I believe Dan set up this to the other day on your set up, uh, ST.

  • So he shows how you can install that.

  • I'm just gonna be using it to serve up our client on dhe.

  • Then another part of the back end is the database, and that usually is drawn like this database.

  • And there are lots of different things you can use for your database.

  • There's metal.

  • Heard of sequel You might have heard of no sequel today.

  • Specifically, we're going to be using a no sequel database called Mongo Devi Mongo D.

  • And, um, you might choose a different database, depending on the application that your building.

  • But today we are going to be using mongo D.

  • B.

  • And really, when you build an application, you pick and choose.

  • Maybe what's the best type of database?

  • But the main idea with the database is that it is persistent storage, persistent.

  • That might not be how you spell it.

  • Store rich, but let's let's taken example.

  • So when you visit twitter dot com, a Web page loads and it's it's very dynamic.

  • There's a lot of stuff in that you have your tweets, you have other people's tweets.

  • You have a place where you can search and you have.

  • You have all of this stuff, but what really happens is when you type a euro into your browser and hit enter.

  • That makes a request to in this case, we'll say, a static file server.

  • So the moment you type in the euro, this will make a get request for some file.

  • In this case, it's going to request index, not HT mo and the static files for her.

  • Well, then respond with that file, which contains all of the HD no contents html.

  • And this is the code that we write and it will get loaded in the browser, and then the user will see it.

  • So this is kind of the first part of what's happening in the full stack.

  • Let's write some code.

  • Okay, so the first thing we'll do is set up the client side.

  • So in this directory here Oh, and before I forget, I did.

  • All of this is on get hub right now.

  • It's just a checklist.

  • But as I code, I'll push it up to get Hub.

  • One of the mods can potentially share this link.

  • I put it in the slack.

  • You can share it in the chat.

  • People want to click on it, but the first thing is will create a client folder and inside of that client folder here in the S code will create a new file and call it index dot HTM Oh, or create a basic HTML document.

  • The title will be Ah, twit.

  • Uh, our me at me Oh, twitter for cats And let's put like a little cat emoji.

  • There we go.

  • For now, I'm gonna get a little bit of this, Lincoln, The script here in the body will add a header.

  • And inside the header, all having each one that just says, um, same thing.

  • Now we're Twitter forgets.

  • Okay, so I have a basic HTML file.

  • This is what the user will see.

  • So in my terminal, I am currently in the client folder and here is well, run live server.

  • So this will start up a static file server.

  • And then when the browser loads, it loads that file that I created.

  • So if I changed that file, um, let's add more emojis cat another cat and save it live server automatically refresh, But the browser will make a get request for this file.

  • So let's just see it happen.

  • I put this you Arlen.

  • I hit.

  • Enter.

  • That's a get request to get the HTML, the browser receives it, parses it into the dome.

  • Lots of stuff happens, but ultimately this is what the user see.

  • So so far, we're, uh, at that first line in the diagram.

  • Okay?

  • And let's set up like a basic skeleton of a website.

  • So for this skeleton, I'm gonna be using this thing called Skeleton CSS eyes a very basic CSS framework.

  • There are a lot of other CSS frameworks out there.

  • Bootstrap is a really popular one or materialized.

  • Basically, it gives us some default styles so that our site looks good by default.

  • It's not like this ugly, fucked.

  • So let's grab this so I can just grab this CSS file and then in here.

  • I'll add a link to it.

  • There, that end, save it.

  • And if you go back to the browser looks a little bit nicer.

  • So this CSS framework automatically adds in fun and different styles to make it look a little bit better inside of the browser.

  • Let's add our own styles as well.

  • So right below here, I'm gonna add a link to Stiles Nazi SS.

  • And then in the folder, I'll create a new file called Stiles Nazi SS and let's just say the header.

  • Let's give it a class So this H one will give this a class of title and then, in our CSS will say, Title has textile line of center, and that should sent her the text.

  • And if we go back to the browser there is, it's in the center.

  • So just to talk about what's happening so far in the diagram, you'll notice that R.

  • H.

  • D mo file has a link to a CSS file and a link to another CSS file.

  • So what happens is the browser will load the file, and then it will see those links, and then it has to go out and request more files So let's look at the diagram here.

  • And so basically it first loaded the request made was made for the ht mo file.

  • Then it needed more things.

  • Basically, the browser parsed it and said, Oh, you want a CSS file from get skeleton?

  • Oh, you want styles?

  • That C S s ome or requests went out.

  • So there was a request here to get the styles Nazi SS.

  • And then the static file server, which is live server responds.

  • The browser takes that and then loads it in and we see some beautiful styles and they're applied to the documents.

  • So every single thing that's happening here is part of a request is going out to a server.

  • So now that the pages loaded the at this point, it's just static.

  • It's just sitting there.

  • We're seeing what we see.

  • It's not communicating to any servers anymore.

  • So what we want to do is we want to make it so that the user can type in their view and then send it off to the server.

  • So let's get that going here.

  • So we created a header.

  • We created the form we brought in our CSS.

  • Um, well, having credit form.

  • We will create the form, any lingering questions or anything like that.

  • Okay, so in here I'm gonna add a main area.

  • And in here, I'll add the form typically forms or what?

  • We used to get user input.

  • So if you've ever typed into a text box or something like that, it is a form.

  • So on this form, I want a label.

  • This will be for user's name, and then I'll add an input.

  • And this will be a text, and we'll give it an I.

  • D.

  • Of name and we'll give it a name of name and let's see what we get.

  • So by default, we get a nice little input box on the screen.

  • Let's give it some styles so built into skeleton is this class named you fool with and you'll notice that the input went all the way across.

  • But I don't want T to take up the whole screen, so let's add some more styles.

  • Let's call this forms.

  • Give a class of like you form, and in here in the styles will add a new one from you form.

  • Let's say the width is like 50% and it has margin zero and auto.

  • So it should center.

  • There we go.

  • So our form is, like, nice and center on the screen, and that's for the name.

  • And then we also need a text area for them.

  • You Okay, so the four here will be called this content.

  • We'll say this, mu, the idea will be content.

  • The name will be new.

  • Sorry.

  • Content.

  • And instead of an input, we want a text area, and that gives us an area where they can type in their content as well.

  • Okay, last thing we need is a button so that they can submit this form and send the data to the server.

  • So we'll just say sinned.

  • Your you input little cat in there like that.

  • We get a nice little button.

  • Another thing built into skeleton is this button primary class, and we'll give it a nice blue color.

  • So let's make this button dash primary.

  • Then we get a nice little button, we can click.

  • Cool.

  • Um, so we have this form the user can type in here, but now we actually want to send the state of somewhere.

  • But let's look back at our checklist.

  • So we created a form we have name.

  • We have content.

  • We've listened.

  • We've set up for whipping all the inputs.

  • But now we actually need javascript to listen for when this form is submitted.

  • So here's what I'll do.

  • I'll create a new file.

  • Let's call this client that Js I'm gonna be creating other jobs, got files.

  • So just to make sure we're clear as to where we are when we're working on them, I'm gonna call this one client a Js, and I'm gonna go ahead before I forget Will add a script at the bottom here that will bring in our client s.

  • And just to make sure it's working, let's just log hello world and back to the browser.

  • Open the deaf tools and it is working.

  • We get to the hello world log.

  • So now we actually want to listen for when the user clicks this button so that we can grab all of the data that's in the form.

  • So first thing I'll do is get a reference to the form by using a document document dot query selector and then with query selector, you can pass in any valid CSS selector to choose some element on the page.

  • So in this case, I want to select this form.

  • So a valid CSS selector for that is just a passive form and that will give me the form.

  • So part of our objectives were to differentiate between client side JavaScript and server side javascript.

  • Any time you see document that is client side jobs because we're actually, um, interacting with the web page with the thing that the user is interacting with.

  • So we have the form.

  • Now we wanna listen for win the users clicks submit.

  • So we're gonna add an event listener for the submit event will get access to the event.

  • And then for now, let's just log out.

  • Form was omitted.

  • Let's see what happens.

  • So my name is C J.

  • My Mu is hello World World world go and you'll notice it tried the law.

  • But then the page refreshed and you'll notice the Ural changed here.

  • So by default, when a form is submitted, the browser automatically tries to send the data somewhere.

  • But we don't want that.

  • We want to stop it from happening so that we can handle it with javascript.

  • And for this we can do prevent default.

  • And now that we're preventing the default action, the data won't go anywhere.

  • We're basically telling the browser Oh, no, we want to do this with javascript.

  • So here, let's try to log form submitted and then hello World Click Submit.

  • And it says forms submitted.

  • Cool.

  • So we've tapped into when the user clicks this button.

  • Now we actually want to grab the data from the page.

  • So to do that, I'm going to use form data.

  • So form data is built into the Web browser and it works by passing in a reference to the forms.

  • Because I already have this form variable.

  • We can pass it in here too, form data.

  • And now we can grab some of the user input from the page.

  • So specifically if I want to grab the name here because I have the name set up as name, I can say, say, name is equal to form data dot get and passin game.

  • And if I also want the content, I set up the name here to be content so I can say content is formed, data dot get content.

  • And now I have what the user typed into both input boxes.

  • And so let's just create an object called a mu and inside of it, we'll have the name and the content and let's just log it out.

  • So now when the form is submitted, we should see an object.

  • Hello, world.

  • Go.

  • So we have the object.

  • But remember, we're still on the client side.

  • We haven't actually interacted with our dynamic server just yet, so let's go back to our checklist.

  • We are listening for the submit.

  • I guess one thing we'll do is when the former submitted.

  • We want the user to know that something is happening, like the data is going somewhere.

  • So I'm actually gonna show a little loading image.

  • So I have this loading Jeff here and we put it in the client and in the HT mail.

  • I'm gonna have a div regular the form Just give this a class of looting.

  • And inside of here I'll have an image.

  • Its source will be loading dot Jif and by default, should like show on the page.

  • There it is, But I want to, like, center it first.

  • So in my CSS, I'll say loading.

  • Um, actually, let's just give it the same 50% with as the mu form.

  • So that way it should automatically pop up in the middle.

  • Almost good night for me.

  • So the this is showing automatically when the page loads but actually want to hide it the moment the page loads and then only show it whenever we've submitted the form.

  • So let's grab a reference to it.

  • So I'm gonna call this the loading element.

  • This will be document dot query selector and we need to pass in some selector to get access to this div.

  • In this case, I can tell it to grab the element with the loading class and win the page loads.

  • I'm just going to hide it.

  • So to do that, I can access its style So loading element dot style and said its display to none.

  • Okay.

  • And so now when the page loads, the loading actually hides.

  • But what we want is when we submit the form, we will hide the form and show the loading.

  • So here's what I'll do all set floating to be displayed and then all set the form to be hidden.

  • Okay, so my name is C.

  • J.

  • My muse.

  • Hello world.

  • And when I click submit we're logging into the council were showing this loading image, but now we actually need to send the data somewhere.

  • Someone is giving me a tip.

  • Ellis saying Center the Jif with textile line center on the container.

  • Let's do it.

  • So in here.

  • Actually, I specifically want to say loading has textile line center.

  • Yeah, I think so.

  • Actually, I just turned my mike back on a couple interesting questions.

  • No seating Using query selector, somebody somebody asked, Well, why don't you use on Click?

  • Or I know there's like a document that get element.

  • Bye, etcetera.

  • Can you talk a bit about why you'd use Quarry selector?

  • Definitely.

  • So, Ultimately, what I want to do with this line of code is select some element on the page.

  • So there a lot of different ways to do it.

  • There's get element by i d.

  • There's get elements by class named Get on my tag name.

  • Um, there's if you've ever used J Query.

  • It's just like dollar sign and putting the selector you want in there.

  • I mainly use this because it's convenient, like I know what specifically what selector to use to select it, but at the end of the day.

  • This is just grabbing an element on the page so you can use whatever you're comfortable with.

  • Well, that's a good question.

  • Other ones, they're worse than where I was.

  • One person asked.

  • Does u E I think there were referring Thio.

  • This is Ahmad, in the chat asked.

  • Is using a framework affect performance?

  • And I think that was referring to the CSS framework.

  • So, like it is it like a big thing toe low.

  • Does it make the page run slower?

  • Other considerations there?

  • Definitely.

  • So I wouldn't say it makes the page run slow, but there will be a little bit of time before the styles will kick in because the browser does have to download the CSS file.

  • But once that's downloaded the CSS file and it's up in going, you're good to go there, Um, and a mental note.

  • I don't think we need to do this now, but people are in love with your color themes.

  • I noticed your the consul on the browser was like a dark themes, maybe a little bit at the end.

  • Maybe good show.

  • It's like what fonts?

  • Theme setting.

  • So I totally glossed over this.

  • So on my channel, I talk about you, J s.

  • I talk about react to talk about a loan, a lot of other front and frameworks.

  • And right now I'm doing all of this with what we call Vanilla Java script.

  • Meaning I'm not.

  • I haven't added me, added any Java script libraries, really?

  • To just show how all this stuff is done manually, But typically all use something like you, J s which which fits in here on the on the diagram.

  • What kind of show?

  • Where specific technologies fit in.

  • And if you have any questions about like, where does this technology live?

  • Throw that in the chat and we can put in the diagram to Okay, it's looking our checklist.

  • We're hiding the former showing the loading.

  • We got the data form, we've logged it.

  • And now we have completed two objectives.

  • We have gotten user input on the client, and we have hidden and shown elements on the client.

  • So this is typically how we get user input when you're dealing from some sort of form, and when you're dealing with vanilla javascript, just manipulating the display property of style is how you hide and show something.

  • So we're doing that now we move on to the back end.

  • So before, in the code that we just wrote lick into the chat, you have to click back into the O B S O.

  • This is this is me over here.

  • So the code a code that we just wrote is very important note is running right now.

  • It's running inside of the browser.

  • Part of you can see that red, we have a different color.

  • So we just created that index dot html It's running in here.

  • So what we're about to do is we're going to jump to the back end and write some code that's gonna be running on our dynamic server over here.

  • So let's do that.

  • Okay, So first step, let's create the server folder.

  • Um, now, as I mentioned, there's a lot of different ways to do this.

  • You technically could put the client folder inside of the server folder, but I'm just gonna show them completely separate just to really differentiate between what they are.

  • So here, let's make a server folder and in the server folder, this is gonna be a node project.

  • So I'm gonna initialize this with a package day song.

  • So mpm it dash, Why just gives it all of my defaults.

  • It tells it my name gives in my default license on and then we need a couple of packages.

  • So specifically, we're going to be using express as our framework to listen for requests that come from the client and then respond with the appropriate data.

  • We're also gonna be using a middleware library called Morgan, which will log all of the incoming requests so we can kind of debug what's happening on the server.

  • So we're gonna install Express and Morgan.

  • Cool.

  • Awesome.

  • So those two things installed now we can set up our server.

  • So on the server new file, let's call this index dodgy.

  • Yes, and inside of here, we're going to create our dynamic server are back and application.

  • So let's bring in express.

  • So if you're familiar with no Js, this is how we bring in a library that we've installed with in P.

  • M.

  • So I brought in the express library and first thing we do is create an application and this is just equal to up equals.

  • Express invoked.

  • So now I have an express application.

  • And now we wanted to actually start listening.

  • So if you saw before, right now, I'm using this static file server, which is live server, and it's running on port 80 80.

  • Every computer has many, many different ports.

  • And so we're gonna have to servers running on my computer.

  • One will be on 80 80 and the other one will be on 5000.

  • So I want to start up this backend server, and I'm gonna tell it to listen on Port 5000.

  • And when it's ready to go, I'm going to just log out listening on http.

  • Colon slash slash local hosts 5000.

  • Cool.

  • And so if I run this, we should get a server that starts listening on Port 5000.

  • So before I started in my package Jason, I'm going to add a start script.

  • So this is basically where you can put the command that should actually start up your server.

  • In this case, it's node Index touch.

  • Yes, And by doing this now in the terminal, I could just do in P m start and that will start up my server on Port 5000.

  • So we take a look at it right now.

  • It's nothing.

  • So we have a blink back in application.

  • It doesn't have any routes.

  • So when I try to request, it just says, cannot get that.

  • And so basically, we're going to start defining what happens when a client makes requests to this server.

  • So we'll get our checklist.

  • We created the server folder.

  • We initialized it.

  • We install dependencies.

  • We have our basic set up.

  • Now I'm going to add a listener for when a get request comes in on the slash route.

  • So, as I mentioned earlier, when you just put something in your browser and hit enter, that is a git request.

  • And by default, it's making a get request to the slash wrapped.

  • So what I'm gonna do on my servers, say his river when you get a get request on the SLASH wrote Run this function and we have two variables in here, typically their abbreviated wreck and rez, which I like to do, but they really stand for request and response.

  • And these were two variables that exactly correspond to our diagram.

  • So here this is the request.

  • It's it's what the server is asking for.

  • And then this arrow going from the back into the front end.

  • This is the response.

  • Now, in this diagram, that's with our static file server.

  • But the exact same thing is going to happen with our backend server.

  • So our client eventually will be making a get request to our dynamic server is going to say I want to get all news and the dynamic server will do some processing.

  • It will talk to the database, and then ultimately this thing will respond with a big Jason object.

  • A problem, actually, And Ray, it will respond with an array of Jason objects, various things inside of them.

  • But specifically, the request that the client will make here is the request and the response that the server gives here is the response.

  • I guess I'm using the word in what I'm talking about.

  • But inside of expressed those those two variables mean exactly that.

  • Yes, I have a question.

  • So I'm curious as to why or if it matters, you're still using the live server for the static files is the idea that you would eventually move the static files also like, Why not have the dynamic server also host those static files?

  • Yes, so there are a lot of a lot of ways to do it.

  • You could do that, but I am actually at the end.

  • I'll show how we put this on the Internet on.

  • The idea with keeping these on a separate server is that we can benefit from things like a CD in like, Constant Delivery Network.

  • And so let's say you get, like, thousands of users to your Web site.

  • They're all gonna be requesting the home page from this CD in which hasn't cashed in like thousands of servers all over the world, and so that the initial Web page will load really good time.

  • And so, by having it on a separate server, it keeps it.

  • It makes the incoming request go to different servers, so the request for actual data will be to the server.

  • But when people are loading, the page will load there, and I will show how toe put it onto your totally makes sense holding me.

  • Flip this camera back, I'll be so seamless.

  • Don't even know where there are other questions.

  • Note that No, I don't think I don't think so.

  • So, as I was mentioning, these two variables are represent that, that incoming requests and the outgoing response.

  • So I'm gonna rename these to wreck and rez.

  • And here's what we say.

  • So when the client makes a get request on the slash route, we want to respond with some Jason.

  • In this case, I'm just gonna pass on a message and it says, um, me ao and we'll throw in an emoji like a laughing cat.

  • Cool.

  • So we now have our server set up when it's receives a request on slash respond with a Jason object.

  • Let's try it.

  • So we go to the browser.

  • We now request the data from our server, which is on port 5000.

  • Nothing happens.

  • So when you're working with No J s, um, have you ever modified the files?

  • You have to actually kill the server and then restart it, and then it should work.

  • So we make the request and we get the data back while I'm thinking about it.

  • There's this handy tool called node Mom, which will automatically refresh every time I change the server and we'll install that and basically all run it in any time I changed files on the server, it automatically restart and we should see those changes when we make a request to it.

  • So no daemon is installed.

  • I'm gonna add a new script in my package.

  • Jason's going to create a Deb script.

  • No, mon and, um, just do the same thing.

  • So tell no daemon to run against that file.

  • And now if I do in P m.

  • Run, Dev, that starts up node monitor.

  • Any time I make changes, it should restart the server.

  • So this loads.

  • But if I go back to my server code and at a new emoji like the regular cat and I again make the request and so you might be saying this, but I'm pressing command are some refreshing the page which is making another get request to the server on slash and then I see the latest result.

  • Cool.

  • So we have a basic server set up that's responding to incoming requests before I move on to this next thing.

  • Any other lingering questions?

  • There were some office of semi off topic question not related to Web development, but not exactly what you're coding.

  • So we could save those today, and I think catalog, um, so let's now work on the route for receiving the news.

  • So if we can remind ourselves what's about to happen here, So on the client, um, this will, when we type in C.

  • J.

  • And we type in some message like Hello, world and click send that has to send it to the server.

  • So we're going to create a route that's waiting for that incoming data and, well, actually, then, like, inserted into our database.

  • So let's create the post route.

  • So here will do an app dot post and we'll make the route slash muse.

  • So when the server receives a post request on slash muse, we are going to run that this request handler And for now, I am just going to log the request body wreck dot body.

  • That's Logan and which it happened is when some incoming post requests happens to my server, it will log out whatever the client said to us.

  • Um, so now let's get that going on the client side.

  • So we're just logging it.

  • But now we're gonna go back to the front end and actually send the data to the server.

  • So on our front end, we're gonna use something called fetch.

  • So right here instead of just logging out to the council were actually send that data somewhere.

  • So first thing is, I need a variable to hold on to where is the server that I'm making request too.

  • So we'll call this a p a euro.

  • And this will be http colon slash, slash local host, typical host Port 5000 and, um slash mused.

  • So I'm going to be making a post request against this girl to actually send this object to our dynamic server.

  • So here, we'll use fetch, will pass in the girl that we're making the request against, and then we have to specify a few different options.

  • So in this case, we need to tell it that the method is post when you to say, what is the body of the request In this case, the body of the request of the thing we're sending to the server is the mu itself.

  • This object with name and content.

  • So we're gonna set body to be you on.

  • But also, we need to specify some headers, so we need to tell it.

  • What are we sending it in?

  • This case will specify the content tight.

  • Petr and we'll say application, Jason.

  • So we're telling the server he server in the body of my request is Jason Right now, it's actually not so right now, it is a job, a script object.

  • But to turn it into Jason, we have to use jayson dot string.

  • If I and this will actually take that object, turn it into something that the server can parts and understand, Andi, we should be able to see it on the server side.

  • So essentially, what's happening here is when the Four Miss submitted, we grab the stuff from the form, we put it in an object.

  • We show that loading spinner and then we attempt to send this data off to our backend server.

  • So let's see what happens.

  • So, um, actually, let's let's set up, okay?

  • I have the backing around.

  • I have the form, Let's let's see what happens.

  • We're gonna watch the server side logs because we want to make sure that we're actually seeing that incoming thing that the client is sending us.

  • Let's see.

  • Okay, So my name is C J.

  • My mu is world and throw in a cat and then we try to send it Oh, no, the dreaded cores air.

  • So you may have seen this before, but it's right now.

  • It's saying response to pre fight request doesn't pass.

  • Access control.

  • Check.

  • No access control.

  • Allow origin.

  • Header is present on the requested resource.

  • So therefore, origin 127.1 80.

  • So you'll notice the Web page is currently on 127001 80 80.

  • That is the current origin is therefore not allowed access to the origin, which is on port 5000.

  • So, um, essentially, the browser is trying to protect us.

  • It's doing this on our behalf like it doesn't want Java script to be able to talk to, like, just any server in the world unless those servers actually allow it.

  • So what we can do on our back end is say, I will allow incoming requests from any any any other origin.

  • So to fix this specifically, we're gonna install something on the back in.

  • Now, if you get this error, you may not always be You may not always have your own server.

  • You might get this error because you're trying to talk to some other server, and in that case, there are other ways to get around it or fix it.

  • But in this case, we're going to use a package on the back end which will automatically add this header.

  • This access control, allow origin header.

  • So, in our server folder here, I'm going to install course.

  • So this is a, uh, mpm package, which is an express middleware and then in our server side.

  • So first before, before we move on, let's clarify where we are.

  • See the corsair?

  • A revel in this moment, you will see this error a lot.

  • There are different ways to fix it, but specifically if you're in control of the server, you can install the course module.

  • So we saw it.

  • We recognized it.

  • We remembered pretty much how to fix it.

  • If we're getting control of the server, that was the front end.

  • We were working on code here that sending data to the server, and now we're going to transition back to the backend.

  • So we installed cores.

  • Um, and now let's use it.

  • So the way this works is I can bring in the cores module and then do what's called using it.

  • So I'll say app that use cores and this actually adds cores as a middle where So now any incoming requests to my server is gonna pass through this middleware, and it's gonna automatically add those cores headers to it.

  • So this should get rid of that error, and then we'll see if we're actually logging out what the client is sending us.

  • So we'll go back.

  • We'll try again.

  • My name is C J Mom.

  • You is Hello, world.

  • We'll send it and we see undefined.

  • So another thing that you have to do on the server side is, um, at a middle where that's going to parse incoming data.

  • So right now in the client, we can see on network that this post request is attempting to happen.

  • So it's making a request here.

  • It's sending this data.

  • So we are sending it from the client, but the server can't process it.

  • So for this, we actually need to add the body part, sir middleware.

  • And this is built in to express.

  • I can use, uh, express dot Jason And this is a Jason, uh, body part, sir.

  • So any incoming requests that has a content type of application, Jason will be parsed by this middleware and then put on the bodies.

  • So now we actually should get access to wrecked up body.

  • Let's try.

  • Okay, So name is C J in you is Hello, world.

  • We're gonna add a cat here.

  • Well, not that cat cat.

  • Go and notice the server is now receiving the data.

  • So the client, the user types and therefore the earth, the information they click the submit button, it gets sent to the server, and now the server has to actually do something with it.

  • So let's look at our checklist.

  • Um, we have made sure that we're actually receiving that data.

  • So the moment I press this submit button, the server is receiving it so I can try again.

  • What?

  • And the server is actually receiving that.

  • So we're taking it from the client and sending it to the server.

  • Okay, so we added are middleware.

  • And now we can process this incoming request.

  • So on the server side, we actually want to do some validation.

  • We want to make sure that the client what they're sending us has a name has content.

  • So let's just do well say if is valid, Mu and will pass in wrecked our body.

  • And if it is, then we'll insert into D B.

  • So we'll handle that in a bit.

  • But if it's not, we actually want to respond with an error right now.

  • My client side, like, technically, you could click that submit button without typing anything in.

  • So I want to protect against people just submitting a blink date blank data to the server.

  • So, um, here, I'm going to just do a resident status.

  • Gonna set this, deport you to.

  • And then here I'll do you arrest that Jason and just give them a message.

  • Hey, uh, name and content are required.

  • Cool.

  • And now let's write.

  • This function is valid in you.

  • So is valid.

  • Mu No, not that takes in.

  • So it is a function it takes in, um, you and we want to make sure so if, um you dot name is a thing and mu dot name dot to string dot trim.

  • So we're gonna take the name that they're sending us making a string, trim it to remove all white space and make sure that that does not equal the empty string.

  • So we're gonna make sure that they have actually sent us a name and this exact same thing for the content.

  • So here will make this content.

  • So we're gonna make sure that they're sending us a name.

  • They're sending us some content, and then we'll insert it into the database.

  • And for now, let's just create an object.

  • Let's call this, uh, you and it will have a name which will be wreck dot body dot name dot to string.

  • And then we'll also have a content, and this will be wrecked up.

  • Body dot Content to string.

  • There was actually an interesting question, So I noticed you doing this.

  • Validation.

  • Do you ever use a library or this came from now?

  • Well, Jose, is there any library node module that you know that helps with data validation?

  • You absolutely don't have to do all that code manually.

  • You're definitely so there is like it's literally called express validator.

  • You can search it on in P.

  • M.

  • There's probably a lot of other ones out there, but these are various libraries that just make it a little bit easier to do validation on incoming requests.

  • So this is pretty popular one.

  • Maybe this is not the one, but yes, there are packages that will do that for you.

  • Andi, Also on my street before I've used something called Joy, which is discussing the chat.

  • And I was like I had no idea what they were talking about.

  • Yes.

  • Oh, joy is a library that comes out of the happy team.

  • They're so happy is another framework very similar to express, but you just do things a little bit differently.

  • But joy is really cool library, because you can do, like object, scheme a validation and, like make sure something is a string, and it's often American has, like a very nice way of defining it.

  • I actually found one called It's called.

  • Yep, which was based on Joy is very yes, there.

  • There are lots of them.

  • Initially when you're getting started, I like to just show this to show like what's happening, But absolutely if you if you have an object that has, like, 10 properties, you probably want to use some library like this.

  • Oh, no.

  • Did I just close Crow.

  • I think I did wait, did I Well, look all those 8000 and then local host 80.

  • And what about someone?

  • Also asked something related to securities, but we'll secure those routes.

  • Foreign validations you talked about for me, So yeah, and like missing here in terms of security?

  • Definitely.

  • So the main thing is, you always want to validate what you're putting into your day to days database, and I'm doing that right here.

  • I'm making sure that the data they're sending us has a name and has some content.

  • Um, and then the other thing I'm doing, which kind of glossed over, is whatever they're sending us.

  • I'm saying to string, So when you're dealing with the database, you want to prevent what's called injection.

  • So if they tried to send us like a nested object instead of just a string and I didn't say to string, we could potentially put that into our database.

  • You wanna be very careful there.

  • Um, the main thing is, if, uh if you're dealing with receiving data from a client, you always want to do validation, and you always want to be very careful about what you're putting into your database.

  • Um, they're correcting that I haven't done any validation on the client side, but as long as I'm doing it on the server side No one's gonna be able to put bad stuff into my database.

  • Definitely.

  • Okay, so now we're validating it, and it should.

  • So, actually, if I look at the dub tools and I tried to send this is an empty form, you'll notice I get back in error.

  • I should probably handle it.

  • We don't have time, but if I do send it good data, then it logged it out, and now we're ready to insert it into a database.

  • So let's look at our checklist.

  • We have validated the name and content.

  • It must be a string.

  • It's not empty.

  • If it's not.

  • Ballad were responding with a 4 to 2 error, and we are making sure that it has a name and content.

  • Now we want to put this thing into a database.

  • So for the database, I'm going to be using Mongo D.

  • B Um, and there are a lot of libraries for talking to Mongo TV.

  • Specifically, the library I'm going to be using is called Monk.

  • It's very simple, very easy to interact with a really popular one.

  • People use is called mongoose, but this is way simpler to get set up, so I'm gonna install a package called Monk.

  • And locally, I have mongo d be installed on my computer so I can type mongo that drops me into a mongo show.

  • And I can actually query the database, insert stuff in and interact with it in this way.

  • But as long as you have it installed and running the stuff I'm showing now, we'll allow you to connect to your database.

  • So I installed a monk and let's get it going.

  • So in my server were still on the server.

  • Let's create a connection to the database.

  • First thing is, we'll bring in Monk, and then we will, um, create a connection to the database using monks.

  • So I'm gonna create a variable called Devi.

  • This will be Monk, and then you pass in.

  • How does it connect to the database?

  • In this case, my database is running on my local machine so I could do local host.

  • And, um, I tell it, what database?

  • So I want to talk to in this case, we'll call it the hour.

  • And right now this just says connect to the Mongol TV on my local machine to a database called Mauer.

  • Simple enough the way Mongo works is with collections.

  • You can have different collections of data.

  • Think of a collection is just basically an array.

  • So ultimately what we're gonna be doing is had will have an array.

  • And then every time someone submits, um, you will put an object in here that has, like, a name, and it has some content, and then Mongo will automatically assign it some unique I D.

  • But basically, every time we put something in the database, this array is just getting bigger and bigger and bigger.

  • But the cool thing about this is it's not just an array, it's a full fledged database. 00:47:02.380 -->

Yes, I muted myself.

Subtitles and vocabulary

Click the word to look it up Click the word to find further inforamtion about it