Transcript from the "Using Express" Lesson
>> Scott Moss: So here's a little hello world getting started with Express just to show you how simple it is. I should've showed you some code of how this will look if you didn't use Express. You would've been like, okay, Express makes sense. But yeah, this is a simple server in Express.
[00:00:19] We require, well, first, we NPM installed Express and then we require it, we make our app and then we set up a get request. So every method we place on this app or most of the methods are HTTP verb, so we're gonna use the get verb. So that means on any get request to /sodos run this function.
[00:00:40] Any post request to /todos run this function. And then, listen on port 3000. So now, we can go local host port 3000 and our server will be there. That's it, just those few lines of code, we had a server with two routes, pretty simple. So yeah, above an example of a basic HTTP server using Express.
[00:01:01] The req and the res that you see in this callback functions right here, they're short for request and response. And they're just objects in the callback that we get access to, and the callback that gives us so much flexibility and so much power. This is also the great thing about Express.
[00:01:20] You'll see this pattern in a lot server frameworks on node, this req response type thing. It's very, very common in the middleware type of thing. And we'll talk about middleware in a minute, but these two objects are very powerful, and this is what you're gonna be working with pretty much the entirety of the course is the request objects, and the response objects.
[00:01:37] Modifying and mutating them to get what you want and to send back appropriate responses from the appropriate requests.
>> Scott Moss: So on the request, we have tons of information about what is trying to access our server through that request object. And then using the response object we have tons of ways to send back a response.
[00:02:01] So here's a little more complicated example. Using like To-dos, all right, everybody's made a todo app. So if we were to do a git request, todo/todos if we wanted to send back the array of todos or a database query of todos, all you would have to say is just res.json(todos), and that's it, and then the client gets it back.
[00:02:26] That's all we would have to do. We could also say response.send which would send it back as well. It also converts it so json. But re.json converts undefined and null to json which technically isn't valid json. But it will convert it. So that's why I use json here.
[00:02:43] You could also do res.send. But this is all you have to do is to send back your json. It's like, okay. When somebody does a get request to todos, take this array, convert it to JSON and send it back. That's what's happening. The next line, little something else is going on here.
[00:02:59] So if we do a post request, which means we're sending data to our server, we want to create something, with the appropriate middleware, again we'll talk about middleware soon, we get access to the actual object that the client is posting to our server by just my saying req.body.todo.
[00:03:14] So the body property on the request and get the todo object. Now we have the todo that was posted to server, add it to our list and then just send it back. And I made a little note here about how res.json and res.send are pretty much the same thing with some small differences, with the undefined and null.
[00:03:36] And then if we have some parameters in our URL, we can access the parameters by doing something like req.params, which is an object of the parameters and the URLs. So in this case we have one parameter and we have given it the name id. So if we wanted to access whatever that was, so anything after todos, that's gonna be considered the id, we can access it by saying req.params and using the name that we specified up here which is id.
[00:04:01] So we can get the id that the person passed in and we can find our todos and then send it back. So again, very, very simple. This is why I chose to use Express. As you can see, it's powerful, but yet it's very simple. And you can also do this yourself without Express.
[00:04:16] It's a lot more work, but you can totally do it. Yeah, Mark?
>> Mark: Do you have to define one function for every request type?
>> Scott Moss: Do you have to define one, well, you definitely need at least one function for each request type, but you can define more than one function for each request type, and that's in middleware.
>> Mark: He kind of follows up, if he wants to do the same thing on both a get and a post.
>> Scott Moss: Hopefully, you're not doing the same thing on a get and a post. But, if for whatever reason you did do that, first of all you wouldn't be following REST, but if you wanted to do that then you would just abstract that thing out and either add it as a middleware or just call it inside this callback function.
[00:04:59] But I think the person who's asking that question is getting into middleware and we'll talk more about that. So yeah, there's this concept of shared functionality that you want to run on specific routes or an application or a stack of routes, before you actually respond back to the client and that's called middleware.
[00:05:16] So we'll see what that looks like. Next thing says middleware, yeah. So, Express uses middleware to modify and inspect the incoming request. So what I mean by that is, you can actually think of this callback function as middleware. Technically, it kind of is. So really, middleware is just a function that allows us to, that's going to be ran in the order that we register with Express.
[00:05:43] And its job is to do whatever we want it to do. It can use any code that it wants. But typically what middleware would do is look at the request object, inspect it, depending on what we're looking for in the request object, it will either pass the execution to the next middleware in the stack or will stop the request and the response cycle right there and either throw an error or respond back to the client and it'll be like, we're done, don't even go forward.
[00:06:06] If you don't know meet these credentials, stop. So, you don't even get to go to this function and send a response back. So we'll see better examples about that later today, but middleware is probably the most powerful feature in Express other than the routing and that's really all Express is.
[00:06:23] It's the routing and it's the middleware, that's all it is. It's very lightweight, if you look at the sourcecode for the Express, it's like one page, it's small. It's really small, it's really easy. So yeah, there are, because the middleware is so powerful, you could think of middleware as like the plugins inside of Express, because of that there's such a big community of third party middleware modules out there that we're gonna be using.
[00:06:47] Express 3.0 had a lot of that stuff baked in because they were using something called Connect. They had a big reliance on this other framework called Connect and then they got away from Connect but they wanted the same stuff, so they took out all that middleware and now it's available third party side but you can still get the same stuff.
[00:07:04] So middleware from parsing, URLs, through handling authentication, we'll be able to use that stuff. And it just makes building with Express super easy. And Express can also be a static web server. So we can serve static assets and trust me, it makes it so much easier than if you did it yourself.
[00:07:21] If you ever had to just write a server manually and serve static assets, if you don't have any experience with that, it can be quite difficult. Setting the appropriate mine types, reading files, dealing with buffers, stuff like that you know, chunks. So it can be quite difficult if you don't have a lot of experience with that.
[00:07:39] Express just makes it easy and one line, it'll handle all that stuff for you.