- [Instructor] When creating routes with Express,
especially with more complex projects,
we may have several routes with the same path,
but using different methods.
Using the method route on our app,
we can chain several methods together
to keep our code clean and easy to understand.
What might this look like?
In our index JS file, I've created three routes
with the same path, slash class,
and they are each using a different method,
get, post, and put.
Let's first start by adding app.route.
So, right above this,
right above our route chaining in our index JS file here,
I'm going to add app.route.
All right, and this is going to take an argument
that represents our path.
So in this case, that will be class.
So, make sure you have the slash class.
All right.
From here, all we need to do is chain our existing methods
to this route.
I'm going to copy everything from dot get to the end
and paste this right after.
So, I will take this,
and then I'm just going to add that right below.
All right, so I will need to remove that semi-colon.
So, we have our first route chained here, which is get.
However, I need to go ahead and remove the reference
to the path within this get here.
So, the only thing that should be there now is request
and response, and then your arrow function,
which is sending retrieve class info.
We will also need to go ahead and remove this semi-colon.
All right so we have the get method done.
I'm going to go ahead and comment this out,
and then let's move on to our post.
So, we're going to do the exact same thing for the post
and the put.
So, I can copy this and then paste it right after the end
of the get method.
So, I'm going to actually put this on a separate line,
and put this on a separate line as well.
Just so you can see a little bit better, the difference,
and before I move on and add the put,
remember to remove the path.
Okay so, and this did some automatic formatting for me here,
but you can see we've got our get,
we've got our post, and our post is doing a response.send
that says create class info.
All right, and then I'm going to just comment this out,
and then we're going to add our put.
So, I'll copy that,
I'm going to remove the semi-colon and paste the put in here,
and then we need to remove the path.
All right, so now, and let me actually just comment this out
before I forget to do that, and save.
So, let's take a look at this really quick.
So, we have app.route and we added the route method
with the path, class, to allow us to chain all
of these methods together.
So, we have our get route that is so it's going to be get
to the class path, and when we hit that,
it's going to say retrieve class info.
That's being sent to the client.
When you do a post to slash class,
you're going to get the response, create class info,
and then when you do a put to the route class,
you're going to get a response of update class info.
Before we move to the browser,
and we're also going to use Postman to look at all
of these responses.
Make sure that you have all
of the original routes commented out.
All right, let's go ahead to Postman and test these routes.
All right, so I'm here in Postman.
I'm just going to open a new tab and we're going to go
to localhost 3000.
So, that was saved their for us, slash class.
And we already have this set to the get method by default.
So if we hit send,
then you should see in the body here retrieve class info,
which is what we would expect to see given how we set
up our route.
Let's try post.
So, the only thing that we'll have to change here is
in the dropdown menu, from get, we're going to change this
to post.
The route is still the same, but when we hit send,
we now see create class info in the body.
All right, and then last but not least,
let's change the method to put, and again,
the route stays the same.
And when we hit send,
we'll see update class info in the body.
Great work.
You've now mastered the basics of Express routing.