This course has been updated! We now recommend you take the AWS for Front-End Engineers (ft. S3, Cloudfront & Route 53) course.
Transcript from the "Serving HTTP Requests with Express" Lesson
[00:00:00]
>> [MUSIC]
[00:00:03]
>> Kevin Whinnery: The first part that we'll start to break down in our application stack is one of the most important components that we have in the stack, which is the Express Web server. It handles incoming HTTP requests and renders HTTP responses with HTML and JSON. It's kind of a core component of this and any other NODE.JS web application.
[00:00:28] It's very similar in style to, say Sinatra, if you've worked with that framework in the Ruby world. Or maybe Nancy from the C# world, or any number of other HTTP micro frameworks out there, in that it is very minimal. And it's also by far the most popular HTTP request handling library out there.
[00:00:55] I'm loath to call it a web framework because it's not really a framework. It's a module that's really good at routing HTTP requests to a chunk of JavaScript code that you would like to run on the server. Unlike a framework like Rails or ASP.net, NVC, or Spring, batteries are absolutely not included with Express.
[00:01:18] Anything you want Express to do, you have to install as additional middleware or some other add-on to the software. So there's good things and bad things about this. The good thing is that you never end up with more bloat in your application than you absolutely require. Because you're consciously choosing every single piece of software that's executed when you're handling an HTTP request.
[00:01:47] The downside is that there are no intelligent defaults. So if you want to do something like, I don't know parse incoming post requests from a client, you need to install an extra piece of middleware to do that. Which we have in our our sample application. So there's good things and bad things about that design.
[00:02:08] The reason why I still like it very much today is because it's not magical. It's very easy for me to reason about what this piece of software is actually doing. And the reason for that is it's all based on a middleware stack. Before we go any any further, does anybody know what this picture is a reference to, by any chance?
[00:02:28] No? All right, so what you all need to do today when you go home is fire up your Kindle and download Ready Player One. It's the name of the name of the book. And this is the stacks, which is a vertical trailer home, which is a trailer park which is featured in Ready Player One.
[00:02:48] A fantastic book you will not be sorry that you did that. But the middleware stack is all Express is. It's just executing a set of JavaScript functions to process an incoming HTTP request, which is coming into your web application. So when your web app receives a HTTP request on port 3000 or wherever it's running, usually what happens is Express will execute something called the middleware.
[00:03:17] And that's just a fancy name for a JavaScript function, which takes in an HTTP request, execute some logic and either returns a response or passes it ahead to the next handler in the middleware stacked. So, in a typical Express application, you have some number of global middleware, they're gonna run on almost every request or every request.
[00:03:41] And we'll see how we can figure that here in a second. We'll also have some specific routes in our applications. So we'll wanna handle specific URLs with specific logic, for maybe getting a to-do item out of our database or executing other logic when a certain path is requested within the web application.
[00:04:03] And then if the global middleware and the application routes are unable to process the request. Or, God forbid, an error happens during the execution of those middleware, we can have other middleware that's lower down in our stack that will catch errors like a 404 not found. Like, if we don't have a handler to handle a certain kind of request.
[00:04:28] Or a 500 error if one of our other middlewares throws an exception. So we have this stack of JavaScript functions that are executed sequentially. At some point, someone is going to return a response of some kind, hopefully. It's kind of how this works. And if you've done any servlet programming or developed Rack applications for Ruby, you might be kind of familiar with this concept of a middleware stack.