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
Middleware is software that sits in the "middle" of other parts of your application -- in other words, it's programming that runs after a request is received but before a response is sent back. In this video, learn how to write your own custom middleware.
Express is referred to as a routing and
middle way framework.
0:00
Routing lets our app do different things
and display different information,
0:03
based on which you are at the browser
requests, and how it requests it.
0:07
So sending a GET request to the slash
login route leads to the login form.
0:12
While a PUT request to slash login,
tells the app to authenticate the user
0:17
by comparing the form data
against records in the database.
0:21
Middleware is software that sits in the
middle of other parts of your application.
0:25
In other words, it's programming that
runs after a request is received, but
0:30
before a response is sent back.
0:34
We've already seen a few
examples of middleware.
0:36
For instance, most express apps ours
included use the body parser module
0:39
to convert incoming requests
into a format that's easy for
0:44
a JavaScript program to use.
0:48
The Express app gets a request.
0:50
Body parser takes the body of the request,
makes it readable to our program
0:53
then hands it off to the next
part of the Express application.
0:57
It sits in the middle
of that request cycle.
1:01
Another example of middleware in our
application is the Express session module
1:04
we used to track sessions for
logged in users.
1:08
This is called application
level middleware,
1:11
because it makes sessions
available anywhere in our app.
1:14
You'll often use more than
one piece of middleware.
1:17
In fact, you can chain together
multiple middleware functions
1:20
that run one after the other.
1:23
In Express, middleware is a function
that has access to the request and
1:26
response objects.
1:30
The entire process from
request to response in Express
1:32
is called the request response cycle.
1:35
A typical piece of middleware is
a function that accepts three arguments,
1:38
the request object, the response object
and a third parameter named next.
1:42
Next is a function, and it represents
the next piece of middleware,
1:48
the next function in
the request response cycle.
1:51
In our app, we've added a bit of
middleware to make sure that the user ID
1:54
property of the session is
available to the application.
1:58
This middleware function assigns a user
ID to the current user property.
2:02
When it's done,
2:06
it calls next, telling express to move
on to the next piece of middleware.
2:06
When writing custom middleware,
we must always include a call to next.
2:11
Let's take a closer look at the middleware
we're already using in our app.
2:18
If you look in the app.js file,
you can see multiple app.u statements.
2:22
These add middleware to the application.
2:28
Some are third party middleware,
like session and body parser, and
2:30
some are built into Express like
Express.static which lets a sort of
2:36
static files within a specific directory,
like images and CSS files.
2:41
Also, towards the bottom of the file,
there's error handling
2:48
middleware that defines middleware
function in line as an anonymous function.
2:51
It accepts an error object and
serves that to the user.
2:56
You can see that the error handling
middleware takes four parameters,
3:00
the usual request, response,
and next parameters but
3:03
the first argument is an error object.
3:08
There's also router middleware.
3:12
That's a function you add
to particular routes.
3:14
You can find links to more
information about Express middleware
3:16
in the teachers notes for this video.
3:19
Next, we'll write custom middleware for
our user authentication system.
3:22
Specifically, we'll create router
middleware to redirect visitors
3:26
based on their logon status.
3:29
You need to sign up for Treehouse in order to download course files.
Sign up