- [Instructor] Even though the phrase route handler
may be new to you, you've actually been using route handlers
for much of the course thus far.
Route handlers are the blocks of code that handle logic
for your routes.
This can be in the form of a function,
an array of functions, or combinations of both.
In the route handlers that we've created so far
in this course,
we've kept them fairly simple with a single callback.
We'll dive into this in our editor in just a second,
but for more information about route handlers,
you can definitely reference the Express documentation
for more information and more detailed and advanced ways
to use route handlers.
If we look at our first route,
our get at the root of the project,
our route handler here is a function with a response.json,
a single callback function.
It is perfectly fine if your route handlers are simple
and straightforward.
In fact, you want them to be as much as possible.
However, there are methods that we can incorporate when
it's necessary to have multiple callbacks.
These opportunities are rare,
and for the sake of this course,
you should just be familiar with how they work
at a basic level.
In order to add additional callbacks,
we can use the function next.
Next is not specific to Node JS or Express.
It's actually a third argument that can be passed along
with the request and response,
and it invokes the next middleware function in the app.
Much like we could name request and response anything,
and I do not recommend you doing that,
next could also be named something different.
However, to avoid confusion
it is important that you always name it next.
Let's go ahead and build a new route using next.
For simplicity's sake,
go ahead and copy our first get route,
and paste it right below.
I'm going to go ahead and make a note here just
for our reference later and say, this is get with next.
We will have this route's path be next.
So, we'll change that here,
and then we're going to need to add next as an argument
after request and response.
We can go ahead and remove this response.json,
and we're going to replace it with a console log
that says the response will be sent by the next function.
All right, and then right below that,
we're going to add the next function.
Great.
Now we have our first callback function set up
within our route handler.
Let's create the second callback.
After the closing curly brace, add a comma,
and hit enter to go to the next line.
Let's go ahead and add our request and response arguments
and set up our arrow function.
Great.
Within this function,
we're going to send a message to the client that says,
so we're going to log, I'm sorry, we're not going to log this out,
we're going to use the response,
and we're going to use the method send to send this message
to the client.
Which is, I just set up a route with a second callback.
All right.
Make sure you save that,
and then let's go ahead and check out our browser
to see what that looks like.
In our browser, we're on localhost:3000 right now,
and we set up this new route at /next.
So, if we hit enter and load that,
we'll see the message that we sent in our second callback,
which is I just set up a route (laughs)
with a second callback.
Excellent.
So, you're now familiar with how
to create routes using a variety of different types
of route handlers.