You need to be signed in to perform this action.
API Tutorial
Transcript
English (Auto-generated)
Hello, My name is Ben and today I'm going to show you how to write some simple api middleware functions using javascript. No Js And uh and express to start out before hopping right on to express. I'm going to show you how to do it without using express just with using the node Js library. So I'm gonna assume you have your project set up node installed and everything like that. So to start we could say var http and just require node http module right out the higher creates server method and explain it line by line by line by line. Mhm. Okay. So first we're using the http module we required here, we call the create server method and if you if you're using um the visual studio code I. D which I would recommend. Um If you just have to create server here, it'll give you um a quick rundown of how the method works. So we see it takes a request listener, we put in a function as a parameter here that takes requests and response parameters here we are writing down we are setting the parameters of some of the response headers we get. So this 200 is the status code and this is the content type parameter. We're sitting into text plain we're writing Hello world. And then we're ending so end is just letting letting uh letting the app no that we're no longer sending Um sending data with this request and we're listening on Port 80 80. Well we're listening on Port 80 80. Excuse me. So to to run this and see how it works. Just go down to the terminal here. If your terminal is not open, you can just go to review um view and then terminal that help helpful. Shortcut, shortcut there and just run node um for us it's server, Js but it will be whatever the name of your files could be app dot Js or whatever you decide to call it. So we run that um local host here and there we see um how the world being printed out with our residents are right line. So what about the first line here with the status code in the content type? Well, to see that um on chrome you can take control shift i it'll open up the developer console for you um head over to network and just hit refresh and then hit local host. You'll see here the status code is 200 and the content type is text plain just like over here. So to really the idea, let's just modify something Nonsense just to see it change and we'll recheck the server. So 300 and text is something There we go. status 300 content type. Text something Okay, So you might ask. Okay, well we we we can do this without even using express why use express at all because it makes things easier anyhow. So assuming you've installed Express just npm npm note, install Express I believe. Care of our expressed similar to how we require the http module. We can fire Express but now you do far as equals. Um Was it again it's Express. Listen any a this as all of this here and there's a lot of other things that express. Does that makes things more simple but um the next season we're going to do one of those things that express simplifies for us is middleware functions. Um So post and get requests uh whenever um in an app you want to communicate with the database um you want to send receive or change information in the in the database like data that's sent to and from the database. Um all that um all the interactions you handle will be written and you're a P. I. A. P. R. Stands for application programming interface. So there will there will be a bunch of scenarios that you'll be wanting to handle for whatever kind of app let's say for example um a user wants to log in. I will be uh you would usually have um route handle for a login. Good request take a function all back function. So a user wanting to log in. You're making a request for data from the database. So you'll right a method that handles a get request that has login in the route. So get request is used to just requesting data from the database. You're not changing anything deleting or updating. Your just asking hey can I have this information oh of the details in the query you're making from that database are in the U. R. L. Um That will be important later and I'll I'll show you how but for now let's just let's just make the route. Um Just just just back slashes to make things simple. Um And we'll say um what should we send let's just say ready to start to send hello world the last time. Let's see how that works. So you just need to restart the server and same thing as before. Um Yeah okay so if you want to change or update data in the database you'll want that will be a post request. So let's we can write post middleware functions the same as get so let's try that same same route. We won't have a conflict or anything like that because there um different different kinds of methods and say sorry past post request. See what happens. Only a post request here. Nothing happens. Why doesn't anything happen? Because with a get request this U. R. L. In and of itself is a get request. When you type in google dot C. A. And hit enter you're making a quest um to a database for information. You're not changing anything. You're just a request. So the U. R. L. In and of itself has everything you need to let someone know that this is a get a get request is happening. So this method fires off. We don't want to me Colin um But regardless we don't have anything that lets us know a post request is actually happening. So this never fires um, uh, post method um, the data is sent to the server with post is stored in the body of your request. So you need to specify usually in in the in your in your code somewhere and manually make a post request. It's making a post request through the U. R. L. Isn't straightforward and you usually don't or can't do it. You would do something like somewhere else in your app. I will manually make the post request. We can manually make make a post request in a get request. So a get request fire can can do something to trigger a post request. So let's see how like I said it's it's triggered because the data is in the body of the http request. So let's see. Mm hmm. See here Our host to seven. Thank one method will specify as a post e. Host is 127001. That's just the default for your machine I believe. Um see what happens if to use it. This we need to really require here. Mm hmm. What else do we need? Um, so we have a we have a request. We haven't actually changed any anything yet. Let's let's change that. Let's um, we have our post request object. Let's try writing some stuff and stuff. Let's try. Okay. I have to end our post request. We have raised that Senate and I'm not sure if that's necessary just in case I have this here. Okay. So it's like for the variable. My post request, send it to an http request object have as a parameter. Take this object which specifies all the all the variables and properties for us and then write some stuff. So a post request is created. Some stuff is written. So the body is is being is being changed the end and bob's your uncle. Let's let's see see how this works. We don't actually have residents and won't show up. Sorry about that log. Ah Here we go. Test post requests so we know that this method is firing off. So yeah, that's all for now.