Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
The `next` function is an important part of middleware. Learn why and when to use it.
More about closures
- Understanding Closures in JavaScript (Treehouse workshop)
- Closures (MDN articles)
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Next is an important function in Express,
so
0:00
let's dig a little bit deeper
into what it is and how it works.
0:04
First, next signals the end
of middleware functions.
0:09
Here's the state of the code
we left off in the last video.
0:14
I'll change the two functions
to log strings again.
0:17
The first one to log, Hello.
0:22
And the second one will log, world.
0:30
Let's see what happens when I remove
next from the first function.
0:37
I'll open up a new tab and
try and access the app.
0:42
The page is not loading.
0:48
If I switch to the console,
hello is logged out, but world didn't.
0:52
That's because the app hangs when
middleware is not closed out with a next.
0:58
The app is waiting indefinitely for
next to be called right here.
1:04
Express relies on the next function
to know when to move forward.
1:11
I'll restore the next function.
1:16
And let's see the browser.
1:22
Great, it loads, but next isn't the only
way to end a middleware function.
1:25
For example,
let's look at the route handler here.
1:32
We're sending a response to the client
here, this is middleware too.
1:38
Next isn't here because
we're ending the function
1:44
by sending a response to the client.
1:47
We've talked about several ways to
send a response, you can use send,
1:50
render or json methods for example.
1:55
The main thing to remember is that you
end middleware by either calling next or
1:58
sending a response.
2:04
Notice, apart from the top three
lines here, And these bottom three.
2:10
The entire app is actually
made of middleware.
2:21
Let's walk through the app in light
of what we've learned so far.
2:33
When a user makes a GET
request to a route, say hello,
2:38
Express runs the first piece of
middleware, the body-parser.
2:42
There's nothing for
the body-parser to do, but it does run.
2:50
Then the cookie-parser runs.
2:55
Now, Express runs this middleware
function logging out hello.
2:59
When next is called,
this piece of middleware
3:04
is executed and world is logged out.
3:09
Now, Express checks to see if
the URL matches the root route,
3:18
it doesn't so this middleware is skipped.
3:23
Same with slash cards.
3:26
Express finds this URL does match though,
so it executes this middleware,
3:31
then render is called, and then
the middleware function terminates itself.
3:39
The request response cycle also ends here,
and the response is sent to the client.
3:46
This process happens for
every request Express receives.
3:52
Understanding Express' execution flow
is really helpful in building and
3:57
troubleshooting apps.
4:01
There's one other question you might be
having with these third party middleware
4:04
were using.
4:08
Where are the request and
response objects?
4:09
How is next being called?
4:12
Notice, these functions are being called.
4:14
They are called as Express sets up the
server and returns middleware functions.
4:19
An outer function that
returns an inner function
4:25
is a common occurrence in JavaScript,
and it's also known as a closure.
4:28
Check the teacher's notes if you
want to learn more about closures.
4:33
There's one other use of
next I want to show you,
4:37
which is to help Express handle errors.
4:40
Let's look at that, in the next video.
4:43
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up