Placeholder Image

Subtitles section Play video

  • today we're gonna build your very first rest a p I as quickly as possible.

  • Let's get started now.

  • Now, in order to create this rest a pia we're going to be using know Js Express and Mongo D B.

  • In order to get started with that, we first need to run N p m in it.

  • So we just typing and PM in it.

  • And we just want to enter a bunch of times in order to get all the default values.

  • And as soon as we do that, it's going to create a package that Jason for us next, we wanted to stall the dependencies that we're gonna use.

  • So our dependencies, we could just hide N p m I.

  • And our first dependency is going to be expressed and then the next dependency that we're going to be having his mongoose, which is going to allow us to interact with Mongo to be easily express, is really useful for when we want to create an application using no Jess because it's just much easier to create a Web application using express than standard Js.

  • Next, we want to use MPM to install her development tendencies.

  • So we could say in P M I.

  • And we just want to say Dash Dash, Save Dev, which is going to make these be development dependencies that do not get installed in production.

  • And we want to use Di e n v and we also want to use Node Mon Now Dottie envy is going to allow us to polling environment variables from a dotty and V file and knowed Monte will allow us to refresh our server every time we make changes.

  • But I have to manually ended and restart it.

  • Now that we have all those installed, let's remove this test script and create our own script called Dev Start which will allow us to start our server using knowed monte.

  • And we just want to type in node mon, followed by the name of our script which would appoint a call server dot Js.

  • We can say that and actually create that new file called server dot Js.

  • And here we go.

  • This is where we're going to put all of our different server code and before we get started any further, we want to create that Dottie Envy folder while right here.

  • And this is going to have our environment variables.

  • We also want to create a dot get ignore, since we're going to be storing this on, get using, get hub orbit, bucket or whatever you want to use.

  • And we want to ignore our Dottie Envy file, since I was going to have sensitive information on.

  • We also want to ignore our Node Modules folder because this could be installed and created by just running NPM install whenever you download the code from get home or wherever it's stored.

  • Now, with that out of the way, let's greater actual server and the first thing we want to do is we want to require express.

  • So what does create a variable, which is called Require Express just like that?

  • And that's going to pulling the express library.

  • Next, we want to create a variable called App on.

  • We just want to run on express function just like this, and that's going to create an at variable, which we can use to configure our server Next.

  • We could just say ap dot Listen, tell what the port we want.

  • It was none in our case, 3000 and we're just going to pass it a function here and this gets run whenever our server get started.

  • So we're just gonna log out that our server was started, so we'll say council about log.

  • And we just want to say that this server has started.

  • Now, if we say that we run that method that we created earlier, which is NPM Run, Davis, start it.

  • Enter.

  • This is going to start up our server using note, Mon.

  • As you can see, it says server started down here and we could go to this app local host 3000 and our server's gonna be running there.

  • But right now it doesn't do anything.

  • So let's make our application actually do something next.

  • We want to configure Mon Goose to connect our mongo DP database.

  • So we're going to require that Leiber We'll just call it monkeys and we're gonna type in, require Mongoose, which again is going to require the library Montagues and why was to set it up?

  • The next thing we need to do is actually connect or have a diet database soul type in Mongoose Connect, and we want to put in here the string which is our database connection and our case.

  • This is going to be mongo, D B colon, backslash, backslash.

  • And then it's going to be vocal house since we're on local host and then again slash And we want to type in whatever our database name is going to be called.

  • We're gonna call this subscribers because this is going to be a database that holds subscriber information, which is what I arrested a p I is going to handle.

  • And then after that's done, we can just save this and you could see down here we get this deprecation warning saying use new Europe are, sir, truth needs to be passed in because the old one is deprecate ID.

  • So what we can do is we can pass that option into the end of our connect just like this.

  • And now we say that you see that that one in message goes away and we have nothing else to worry about.

  • Also, what we can do is we can actually create a variable hell called D B, which going to be equal to mongers stock connection.

  • And from here we can hook up some events to run when our databases connected to so we know it's working correctly.

  • We could just say D b dot on and what we want to do is on error.

  • We want to log out that there's an air, so we'll just say here this is going to take a function with an air and we can come in here and say console dot there and we can pass it that air this'll just allow us to see if there's a problem connecting to our database.

  • Also, what we want to do is it Once we connect so well, say data D b dot once, which means it's only one runs.

  • And whenever we opened the database, what we want to do is we want to counsel that log saying that we opened the date of this so we could just say connected to database just like that.

  • And now when we say if we should get the message down here, same connected the database, and that's perfect.

  • But one thing that you'll notice immediately is that we have our mongo D B database string inside of our application, and when we deploy our application, we're going to want to use something that's not our local host, so we need to pull this out into an environment variable.

  • So let's remove this right here.

  • Go into our dot nd We could just create a variable, which we're going to call database you, Earl.

  • And we want to set that equal to Armando DVD library right here and now inside of our server.

  • We can come in here and use process dot e n v dot database euro or just whatever you name this inside of your diet and the folder, and this will pull from our diet envy.

  • We'll notice we get an error, and that's because we need to use the Dottie Envy Library.

  • It's all the way.

  • The beginning, we can say, require Dottie envy and all we need to do after require this is called a confit method.

  • So just say, dot com fig.

  • And this is going to load all of our environment variables from our Dottie envy.

  • Now, when we say that you'll see we no longer getting air down here and it says we connected to our database successfully.

  • So now we have a database completely done, our service completely hooked up in listening.

  • All we need to do is actually create routes for our server and also set up our server to accept Jason.

  • So let's first set up our servitude.

  • Except Jason.

  • We can just say after use, which will allow us to use any middle where that we want, which is essentially code that runs when the server gets a request.

  • But before it gets passed to your routes.

  • And what we want to do is we want to use express dot Jason and it's essentially just lets our server except Jason as a body inside of a post element or get Omer whatever.

  • Also, what we want to do is we want to set up our routes.

  • So since we're going to have a subscriber, FBI, we're going to have some routes that are gonna be called subscribers grounds Subscribers Router never get lips.

  • I cannot spell subscribers.

  • Writer.

  • There we go.

  • This is going to route all of our subscriber information.

  • We could just say require and this is going to be inside of a folder inside of her application called Routes.

  • And it's just going to be called subscribers so we can create that.

  • Now.

  • Let's create new folder.

  • This is going to be called grabs.

  • And inside of this folder we want to create a file which is going to be called subscribers dot Js.

  • It just needs to match the name that we added in here for subscribers right here.

  • Now, once we have that, we can go in here and tell our app that we want to use that round so we can say app that use We pass it the path we want, which in our case, we want to use this whenever we query subscribers.

  • So are you are always gonna look like this local host 3000 slash subscribers.

  • And this is going to be right here, this euro, everything that has this euro or anything after it is going to go into this subscribers router which we created right here.

  • So now that we have that done, we need to actually tell it.

  • We want to use the subscribers Ratter, and then we go, our application is set up and working, and you'll see that we get an air.

  • And this is because our router dot use requires middle.

  • Where this subscriber writer right now is absolutely nothing.

  • This is completely blank.

  • So let's set that up.

  • We again, I need to use expressed.

  • So we're going to say require express in here.

  • This is because our entire application uses express.

  • And what we want is we want the router portion of express.

  • So we could just say express dot Browder And this is going to get us the router from expression.

  • And for now, all we want to do is export that so we can save module exports is going to equal to router.

  • This is just going to fix our errors down here.

  • And as you can see, we no longer get any errors.

  • Everything's working fine.

  • But we have no routes actually configured yet, since it is going to be a rest, Phillipe, I we're going to use rest ful in points.

  • And if you don't already have a strong grasp of what rest is and what rest will end points are I have a really short video going over arrest in depth, which you can check out in the cards and the description like below.

  • So now let's start creating our different routes.

  • We're going to want some routes for getting all subscribers were gonna want a route for getting one.

  • We're also gonna want a route for creating one.

  • We're gonna want a route for updating the single one hopes updating one.

  • And we're also going to want around here for the leading one.

  • And let's configure those wraps To do that, we could just say router dot Get for example, which is going to be get.

  • And to get all we just want to use this Blake path right here.

  • And of course, all these are going to take a request and the response, and that is going to be a function.

  • And inside of here, we're going to do all of our code for our different routes.

  • So what's bit all of these rocks created?

  • Forgetting one.

  • We're going to want an I D in here and with a colon in front of it like this.

  • This means that this is a parameter that we can access by typing and request dot Karim's And this would be wolves grams dot i D.

  • And this would give us access to whatever they pass in after the first slash.

  • Next, we want to create our create route, and this is going to be a post instead of agate.

  • So we can say dot post and again, we're not going to need an I d here.

  • Since they're not actually going to be creating it with an I.

  • D.

  • They're going to be creating it on just the general route.

  • Next, we need to do update so coming down here and we're going to be using patch instead of put for update because we only want to update based on what the user passes us.

  • If they only pass us the name of the subscriber, we only want to update the name and none the other information about the subscriber.

  • Because if we used put, it would update all the information of describe her all at once instead of just the information that gets passed.

  • Now, lastly, we want to create our delete route.

  • What is going to be extremely stuff explanatory?

  • This is just going to be a delete.

  • And again, we need an I d.

  • Here as well as on the pad trout.

  • We're going to need an i d.

  • And this is the basic shell of all the different routes that we're going to create now, in order to test these routes.

  • What's great in here?

  • Just a rez dot send, which is just going to send text down to the server.

  • I was gonna say hello, world.

  • So we know that we actually have called this Get out here for getting all of our subscribers.

  • And normally, to test these, you may go into the browser and open it up, but you can't really test arrest a guy very well in a browser because you have nothing to interact with.

  • So what we're going to use is we're going to use an extension in visual studio code, and this extension is called right here.

  • Rest client has two million downloads is incredibly great, and it allows you to call arrested guy directly from visual studio code.

  • You can also use a program such as postman if you want to instead.

  • But I really like being able to do this directly in visual studio code.

  • So we're going to do is we want to create a path here.

  • A file which is going to be should be called route dot rest.

  • You can call it whatever you want.

  • It just has to end in either dot rest for dot http.

  • And in here we can create our requests.

  • In our case, we want to create a give request and we want this.

  • Get request to go to http slash slash local host 3000 that we wanted to be in the subscribers.

  • So we're going to say slash subscribers clips that added a little bit more than I wanted.