Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Understanding Express Middleware!
You have completed Understanding Express Middleware!
Preview
The `next()` function is important in Express middleware. Let's examine what it does and how to use it.
JavaScript Error Resources
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.
0:00
It signals the end of
a middleware function.
0:03
But it's not the only way to
end middleware, as we'll see.
0:06
Next also provides a way to handle errors.
0:10
Here's the state of the code as
we left it in the last video.
0:13
Let's see what happens when I remove
next from the first function.
0:17
I'll open a new tab and request our app.
0:23
The page is not loading,
0:27
that's because the app hangs when
middleware is not closed out with next.
0:29
The app is waiting indefinitely for
next to be called right here.
0:33
Express relies on the next function
to know when to move forward.
0:38
I'll replace next and
let's see the browser.
0:42
Great, it's working again.
0:47
There is another way to end a middleware
function besides calling next.
0:49
In fact let's look at this last line here.
0:53
This is middleware 2, next isn't here
because we're ending this function by
0:57
sending a response to
the client right here.
1:02
There are several ways to send a response.
1:06
Calling the send method is one way but
you can also call render
1:08
if you're using templates or JSON to
send JSON formatted data among others.
1:13
The main thing to remember is you have to
end middleware by either calling next or
1:19
sending a response.
1:23
Notice that apart from
the top three lines and
1:25
the bottom line,
this entire app is made of middleware.
1:28
There's one other thing I
wanna show you with next.
1:33
Applications always need
a way of handling errors,
1:36
users commonly request pages
that don't exist for example.
1:40
That constitutes a 404 error that your
app should be equipped to handle.
1:44
In Express,
1:50
you use next to throw an error by passing
an error object in as a parameter.
1:51
Express will immediately terminate and
handle the error.
1:57
Let's create our own error
in the first function.
2:00
Before I do that I'm going
to replace this bit of code
2:03
logged to the console and will log hello.
2:08
Down here, we'll log world.
2:15
Now I'll create a constant, And
2:23
use the error constructor
passing in a string.
2:29
If you haven't seen the syntax before.
2:35
Don't worry too much about it for
now, it's enough
2:37
to know we're creating a custom error
object and storing it in error.
2:40
You can use error objects to provide
helpful information when things aren't
2:45
working right in a program.
2:49
Look at the teacher's notes if you
want to read more about JavaScript's
2:51
error constructor.
2:55
Let's pass this error object
into next as an argument.
2:56
If we save and refresh the browser.
3:01
You can see that our error was thrown.
3:06
Let's just check the console.
3:09
Here you can see that.
3:11
Hello was logged before
the error was thrown.
3:12
World was never logged because
the program was interrupted by the error.
3:16
Let's get some practice with
middleware and a bigger application.
3:20
I'll see you in the next video where we'll
use middleware to process user input
3:24
from the client.
3:29
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