Express comes with a well-documented API that provides all functionality of the framework. This video covers the general structure and important methods as well as how to look up API functions.
- [Narrator] In this video, we will explore the basic building blocks of Express. Here's a very simple Hello World sample. You don't need to have it open, just follow along with me. Already in this simple sample, we have almost all complements Express consists of on onscreen. On line one, you see Express itself. On line two, we are creating the application object. And then, on line five, on the request handler, you see the request object and the response object.
Let's attach a debugger and step through it to understand better what Express is doing under the hood. For that, I will first put a breakpoint here in line two and one in line six, and I'm starting the debugger. And we're holding here at the breakpoint, and we see that now Express is instantiated. Let's step into that, and we see that create application object basically composes the whole express application, and creates the request, the response object.
And then, it calls app.init, so let's, real quick, step further through that and into app.init and there we see that it just initializes the application with some default configuration. Let's step out of that again and re-return app from here. Next, we define run route, it's a get route, let's step into that now and here you see something that is quite interesting.
In Express, you can use app.get also to set configuration variables. And, here you see how this is done so it simply looks into the arguments to distinguish between, is this a route declaration or are we just setting a configure variable? Then, if we scroll down a little bit, we see on line 482 how our routing function is simply added to the list of routes of this application.
Let's step out of that and then in the end the application simply starts to listen. Let's look into this app.listen real quick and we see that here we simply create a stock HTTP server, but adds the application as handler function for this server. Let's step out of that and let's follow along with the execution.
Next, I will use my browser to open this page and put 3,000 and we're now in the handler for this route. Now, let's look a little bit into the object, into the variables we have here. If you look at the left side we see that we now have this request and this response object and scope. Each of these objects gives us a set of properties and methods to work with and they are very well documented on the Express websites. So, let's head over there. So, Express gives us the application object and the application object lets us define middlewares and routes and also contains a few helpers to set configuration data like, The FUE Engine.
The request object is what we get in our middleware function and it holds information about the request we are currently processing. It contains mostly properties, but also a few messages to inquiry information about the request. The response object is what we use to send to response. Naturally, it contains a variety of methods from template rendering to sending a chasten response. The API also describes a route object that we get where express.router and it contains a subset of methods the application object gives us.
This is all Express consists of, no mater if the application is large or small. Most of its power comes from being extendable via middlewares. In the next video, we will cover a few popular ones.
- Creating a template engine
- Using MongoDB and Mongoose for user management
- Authenticating and authorizing users
- Adding cookies and sessions to Express
- Creating a login form with Passport
- Handling file uploads
- Storing and serving images
- Deploying an Express application
- Securing an Express application
- Running behind a web server