In this video you'll be setting up the very first End Point for our REST API.
Now the goal of this video and the next one is to focus on our endpoints that involve resource creation
so creating new users and creating new tasks.
Together it will work on creating new users then any next video as a challenge.
You'll be setting up the ability to create tasks on your own.
Let's get started by installing a few things we've used before.
We're going to install no demand as a dev dependency and we're going to install express as a regular
dependency.
So down below from the terminal NPM I will first install no demand at the latest version one point eighteen
point nine followed by the Save Dev flag to save node man as a development dependency.
Then we're gonna go ahead and run that.
And before we actually write any code we'll also take a quick moment to install Express into the task
manager app once that's in place we'll focus on creating the Express server and setting up the very
first endpoint down below.
We're almost done installing node man.
Now one other thing we're gonna do in this video is we're gonna refactor the Mongoose file.
We're not going to leave our model definitions inside of here.
We're going to create separate files for those as well.
So we're staying nice and organized as we build out the task manager app.
We'll talk about directory structures that scale well down below NPM I express at the latest version
that is four point sixteen point four.
Now once that's installed will actually be able to use both of those tools.
Let's go ahead and set up the file that's gonna start up our application.
So inside of the source directory we're gonna go ahead and have a new file index dot J S.
This is going to be the starting point for our application.
And this is where we're gonna initialize that expressed server.
Now we also have this Mongo D.B. file sitting around.
That's not going to make it into the final version of our application.
You can choose to leave yours around or you can just download the lecture zips if you ever wanted to
remember what we did there and remember that's all documented in EPD f guide as well.
So I'll take a quick moment to delete Mongo D.B. dot J S I'll be kicking things off in index dot and
J asked by loading in express I'm going to require it.
Grabbing the contents like we had done before then I'm gonna go ahead and create the new express application
contest app equals where we call express exactly like we had set up in our web server project from here
we'll go ahead and define the port.
Now eventually we are going to deploy this to Heroku.
So right away I'll get the port value by looking first at process dot EMV dot port which we know is
what we need to do in order to get our app working on Heroku.
Then if that doesn't exist I'll fall back to something I can use for local development like port three
thousand.
Next up all we need to do is use app dot listen right here app dot listen I'm going to listen on port
and then we can provide our message right here.
Something like console dialog server is up on port then I'll go ahead and just concatenate on the port
value.
Perfect.
So we have the basic server in place.
We're also going to set up our package dot Jason scripts.
We want to make sure we can easily use node man and now that it's installed we can set it up in a script.
I'll be removing the test script and we'll be setting up to we're going to set up start.
And we're also going to setup Deb now the dead script is gonna be similar to what we had before.
I'll be using Node Mohn to start up the file in source that is source forward slash index dot J S now
we won't be using handlebars in this project.
So there's no need to provide that extensions argument where we listed out JavaScript files and HB Yes
files this alone is enough.
Now when it comes to Heroku starting our application it'll just use the node command to start up the
same file that is node source index dot J.
S excellent.
So now that we have this in place we can go ahead and start up our server and go through the process
of actually adding our very first root down below.
I'll be running that Dev script NPM run Dev.
Now we should see our message that the server is up and running on port three thousand and it is.
But if we visited it in the browser we wouldn't see anything because we haven't configured the Express
application to actually do anything.
If we were setting up a get request we would use app dot get to configure that that would allow someone
to access the given route using the get h TTP method.
Now in this case we're trying to use post for resource creation.
And for that we use app dot post.
So right here express provides us with methods for all of the T DP methods we're gonna use.
We also have app dot delete and app dot patch which we'll be using as we continue to add endpoints.
Now right here all of those have these same exact call signature.
So the first is still the path and the second is still our callback function which runs when someone
tries to access that particular route.
We get access to the request and the response exactly like we would of if we were using app dot get
inside of that weather web server we create it right here though we're gonna set up the you are out
for this particular post to request.
Now we're creating a new user here so that would be forward slash users plural sized inside of here.
We can now go ahead and just send back a little test response response.
Dot send testing.
Perfect.
Let's go ahead and save this and get this to show up in man before continuing on.
So over inside of post man I'm going to create a brand new collection right here in the collections
menu.
We have one dummy collection for the weather application.
I'll create a another collection for the task app.
Now there's no need to configure anything in these menus.
We'll talk about some of this later.
For now though we can just create that bare bones collection from here we're going to add our first
request into the task app collection.
I can click the little menu I can then click Create task or add request excuse me.
And once we add the request we'll be able to actually configure it right here.
Let's pick a name for this.
I'll use something like create user and down below it's automatically selected the collection right
here.
Task app which is perfect.
Next up we'll save that brand new request.
We will click it which is gonna crack it open in the post man interface.
Now we're gonna go ahead and configure the U.R.L. to what we just set up.
That's local host three thousand.
This is where our server lives forward slash users.
Now when we fire this off with the Get HDTV method we're not going to get anything back.
If I fire that off Express is going to send back a forum for saying that there is no root handler for
what you tried to access.
We explicitly used app dot post which means that we can only access this via the post H TTP method.
Right here I'm going to click it.
Switching from get to post that I'm going to go ahead and send this off when I do that we'll actually
get our testing message showing up.
So at this point we're able to run the given route handler.
The question is how do we provide the data necessary things like the e mail the name the password and
other attributes we might want to send from the client to the server as we saw a couple of videos ago
when we broke down what makes up an HDTV request.
We learned that we'll be sending our Jason data via the request body now.
Postman gives us a way to configure that via the body tab right here.
So I'm gonna switch over from paradigms which is the default to body now.
By default none is selected.
We're gonna switch that over to raw then from the dropdown we'll switch that over from plain text to
Jason allowing us to send Jason data from postman over to the Express server.
This is where we're going to provide everything we need in order to create a new user.
That would be the name the email and the password.
The other attribute age is completely optional and doesn't need to be provided here.
So let's go ahead and get that done.
I will first set up name as Jason.
So remember we have to have our properties in double quotes as well.
I'll just use my name for this.
Then I'll go ahead and set up email and I'll use Andrew and example dot com.
And after that I'll set up the final one we have to provide which is password and I will use something
like read one to three exclamation mark.
Then I'll use the dollar sign.
Perfect.
So now we have our data set up correctly.
And if I click send we're going to see that the data does get sent off and down below we are still getting
our little message and things are going well.
We have our two hundred status code which means that things went OK from here the question is how do
we actually get that data so we can use it to create the new user.
This is a two step process.
And the first is configuring express to automatically pass the incoming Jason for us.
So we have it accessible as an object.
We can easily use we do that by setting up a call to app use to customize our server and express provides
us with something to pass in.
Right here.
We use Express dot Jason calling that as a function when we set up this one line.
It's going to automatically pass incoming Jason to an object so we can access it in our request handlers.
Right here we can do that using a request dot body.
All I'm gonna do for the moment is dump that into the console so we can look at exactly what we get.
Right here I'll dump a request dot body.
I'm gonna save the file so no demand restarts and I'll fire off the exact same post command request
from the post man application.
Now I'm not getting anything new in the response which is OK but over in the terminal in visual studio
code I should see my data.
And right here I have it.
I have the name email and password exactly as they were provided in postman.
So now we know how to grab the incoming body data and we can use it to actually create a new user to
get that done.
We have to make sure that Mongoose connects to the database and we need to get access to our user model
from inside of this file that's going to require us to do a little bit of restructuring as to what lives
inside of Mongoose dot J S in here.
We're just going to have the code necessary to connect to the database.
We're going to move our models to other files for that let's create a new directory in the source folder
called models.
This is going to sit alongside of the D directory and in here we'll have a single file for each of the
models we want to create.
Right now we'll have just one which I'll call user dot J S and in the next video you'll be breaking
out the task model in much the same way.
So what's going to go inside of user dot J S.
Well the entire user model from the const declaration all the way down to the end after we define all
of the attributes I'm gonna take that I'm going to cut it out of its current location and I'll bring
it over to user dot J S now inside of here we are indeed using Mongoose and we are also using that validator
library.
So both of those needed to get loaded in as well over inside of Mongoose dot J S I can simply grab the
two existing require lines and I can bring them over to the new file.
Now we can go ahead and make sure that we actually export something from this file down below.
We want to export user so other files like index dot J S can use it to create new users.
Way down at the bottom of the file we'll get this done by setting module down exports equal to uppercase
U user.
Perfect.
Now we're done with this file and we can go ahead and save it over in Mongoose dot J S we have a little
extra code validator is no longer being used in this file so I can go ahead and remove the require call.
I also have some example code where we learned how to create new instances of our models and save them.
I'm going to remove that for user and I'm also going to remove that for a task down below for the moment
will leave the task model exactly where it is as we're not going to touch that in this video.
So now what do we need to do.
Well we need to head back to index dot J.
S and load a couple of things in the first thing I want to do is require the Mongoose file.
So from index dot J S that would be dot forward slash D.B. forward slash mongoose.
Now we don't want to actually grab anything from that file by simply calling require.
That's going to ensure that the file runs and it's going to ensure that Mongoose connects to the database.
The next thing we're going to do in index dot J S is actually load user in right here const user equals
a another call to require dot forward slash models forward slash user.
Excellent.
Now with this in place we can create a new instance of the user inside of our route handler and we can
make sure that they get saved correctly.
The first thing we're gonna do is create a new user.
So const user equals a new user.
Now we know the constructor function for our models requires us to pass in an object with all of the
attributes we want to set up and in our case we already have an object that has all of those attributes
as we just saw that is request dot body.
Now from here we can actually go ahead and call user dot save to attempt to save the new user.
It might work it might not.
Either way we want to give it a try.
Down below I'll setup a then call to handle successes and I'll set up a catch call to handle errors.
So dot then with a callback function then dot catch with a callback function and in both we're going
to respond to the individual who made the request.
Now when things go well we're just going to send back the user with all of the additional fields.
The Age has been set to its default and an idea has been generated for us right here.
We'll use response dot send to get that done.
What are we going to send back.
I'm going to send back the user.
Perfect.
Let's go ahead and test this out before actually going any further.
Now in order for this code to run the data we send across needs to be valid.
This data is indeed valid.
We can go ahead and click that send button.
And right here I can see that I have the entire model showing up.
I have all of the attributes we provided including the additional ones that were generated for us.
Now the new user we created was actually saved to the database.
I can prove that by heading over to robo 3 T from here we have our users collection.
I'm going to go ahead and refresh it.
I'll expand to the last document and this is exactly what we just created.
So there we go.
That is our very first rest api.
We're able to send data via an H TTP request off to the Express server using a predefined operation
we're able to perform some sort of manipulation on the database.
In this case creating the user.
Now we still want to handle errors as well.
If something goes wrong we will get access to the error and we'll be able to send that as part of their
response.
To do that we'll use response dot send sending back the error just like this now.
When it comes to sending back errors we also want to change the status code.
So let's go ahead and view the current output.
If we generated an error.
What I'm going to do is provide a password that's too short.
I'll get rid of a bunch of characters leaving just two characters as part of the password and if I go
ahead and send this off I would expect it to fail.
Right down below we are indeed getting the message.
The path password with the value R E is shorter than the minimum allowed length which is 7.
So right here that is indeed a problem and we're getting the error sent back and no new user has been
created.
Now these status code is still a two hundred which is a bit misleading when we're working with rest
api eyes we want to provide the most accurate status codes possible for a complete list of all of the
status codes we can work with.
We can head over to the browser and go to a really neat little Web site.
It is h TTP statuses dot com.
Here we can view a complete list of all of the common status codes we can provide.
Now our HDTV status codes are broken into different groups.
They're all in the hundreds.
We have our one hundred status codes which are informational.
Our two hundred status codes which are for success.
Three hundred four redirection 404 client error and a five hundred for server error.
Now for our rest api we'll be using two hundred status codes when things go well such as when you provide
user data to create a user and we're able to create it.
We'll be using four hundred status codes for when things go wrong because of something the client did
such as providing invalid data and we'll be using five hundred status codes when something goes wrong
because of an error on the server.
So in this case for an invalid password or any invalid user data we want to use a four hundred status
code and we're gonna go ahead and use four hundred for each of these status codes you can actually click
on it to learn more about it.
So right here each of them have an individual page now to set up these status code.
We use the status method on the response.
So right here I can use response dot status to set the status equal to four hundred.
Now it's important that we do this before we call response dot send.
Otherwise the status isn't gonna get sent correctly.
Now let's go ahead and move back over to postmen and fire off that request one more time with the broken
password.
Right here we can see the status was changed to four hundred which is fantastic.
Now we don't have to use two statements to get that done.
We can chain together our method calls so I can use response dot status dot send to send back the error
and that makes this other line irrelevant.
So right here we have the exact same setup using a single line and we'll be taking advantage of chaining
when it's available right here in postman we can send this off for the final time and we get the exact
same result.
We're still getting our response body which is good and we're still seeing our new status code.
Now that's where I'd like to stop before this one.
In this video we set up our very first rest api route the route for creating a new user we saw how we
could refactor the Mongoose file to focus on just connecting to the database and we now defined the
model user in a separate file down below.
We use it inside of our root handler in the next video.
You're going to do something similar but for tasks you'll be setting up a new route for creating a task.
I'm really excited to get to that challenge and give you some more hands on experience.
So let's jump right into that.