
Lesson Description
The "Express Middleware" Lesson is part of the full, API Design in Node.js, v5 course featured in this preview video. Here's what you'd learn in this lesson:
Scott explains middleware as code between other code, handling tasks like error interception and modifying requests. He stresses managing responses carefully, since sending one closes the connection and mismanaged middleware can cause issues.
Transcript from the "Express Middleware" Lesson
[00:00:00]
>> Speaker 1: Middleware Anybody want to give me their best guess of what Middleware is I've heard that term so many times because it's a word that has many different definitions and contexts Like, for instance, when you think of Middleware with React, anybody have any examples there
[00:00:00]
What about Middleware for intercepting HTTP requests on the client to do error handling or similar tasks, has anyone done that before Or what about on the network layer You have Middleware there in Next.js
[00:00:00]
I think there's definitely Middleware, and if you use Next.js, you've probably used Middleware whether you knew it or not because it's literally what I call the backbone of web apps
[00:00:00]
It's one of those words that means so many different things Well, it means one thing but is applied to many different contexts It's kind of like caching, where it's so generic that you have to specify: are we talking database, network, local caching
[00:00:00]
What exactly are we discussing So what is Middleware Middleware is just code that sits between two other pieces of code It's in the middle—that's why it's called Middleware You have two pieces of code, and Middleware sits between them
[00:00:00]
Typically, a good implementation of Middleware would be either a list of functions that you can run in a specific order or a robust implementation where you can do whatever you want in one function
[00:00:00]
In the case of an API, here's how it would work: a request comes in, and instead of going directly to a handler and then sending a response, Middleware would sit between the handler and the response
[00:00:00]
There's no shortage of Middleware we can use or create for different reasons These Middleware have access to the same things the handler has—the request and response objects—which means they can inspect the request, modify it, or even short-circuit a request using the response object
[00:00:00]
One critical thing to understand is that you can't respond to a request more than once Once a request has been responded to, that connection is closed This is different from returning from a function
[00:00:00]
A return is not the same as a response On the code level, JavaScript sees the function as complete, but on the network level, HTTP is still waiting for a response With JSON methods, everything is essentially a send
[00:00:00]
JSON is just a wrapper around send that automatically stringifies the data and adds appropriate headers to let the client know it's receiving JSON It's equivalent to manually stringifying JSON and setting the content-type header to "application/json", which you need to do to properly send JSON across the wire
[00:00:00]
Middleware is powerful because it can respond to a request before your handler does This means your handler might not actually respond to the request if Middleware has already done so
[00:00:00]
You must be careful to protect your handlers from executing if Middleware has already responded, or you could end up with serious routing and response issues.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops