Scott walks through a simple Express example. This example is a basic HTTP server listening on port 3000. The server has a couple routes for handling both GET and POST requests for a todos API.
(Music Playing) - So here's like a little 'Hello, World.' Getting started with Express. This will show you how simple it is. I should have showed you some code of how this will look if you didn't use Express. You would have been like, okay. Express makes sense. For ya'll, this is how, this is simple server in Express. We require, well, first empty and install 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 ACP verbs, so we're going to use the 'get' verb.
So that means, on any 'get' request, to slash to do's, run this function. Any post requests you slash to do's, run this function. And then, listen on port 3000. So now, we can go to local host port 3000 and our server will be there. That's it. Just those few lines of code that we had. A server with two routes. Pretty simple. So, yeah, Above an example of the basic ACP server using Express. The actual rec and the rest that you see in these call back functions right here, they're short for request and response.
And they're just objects in the call back that we get access to. And the call back that gives us so much flexibility and so much power. This is docile. The great thing about, about Express. You'll see this pattern in a lot of server frameworks on note, this rec response type thing, is very, very common in the middle ware type of thing. And we'll talk about middle ware in a minute, but these two objects are very powerful. And this is what you're going to be working with, pretty much the entirety of the course is the request object and the response objects and modifying and mutating them to, to get what you want and to send back appropriate responses from the appropriate request.
So, on the request, we have all, we have tons of information about what it's 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. So here's a little more complicated example using, like, to do's. Right, Everybody's made a to do app. So, if we were to do a get request to slash to do's, if we wanted to send back that array of to dos or a database query of to do's, all we have to say is just response dot Jason.
And pass in that to do's and that's it. And then a client gets it back. That's all we have to do. We could also say response dot sin. Which would send it back as well. And also convert to the Jason, but response dot Jason converts undefined and noll to Jason which, technically, isn't valid Jason, but it will convert it. So that's why I use Jason here. You could also do res dot sin. But this is all you have to do to send back your Jason. It's like, " Okay." When somebody doesn't get request to do's, take this array, convert it to Jason, and send it back.
That's what's happening. Next line, little something else is going on here. So if we do a post request, which means we're sending data to our server, we want to create something. With appropriate middle ware, again we'll talk about middle ware soon, we get access to the actual object that the client is posting to our server by just saying rec dot body dot to do. So the body property only requests and get the to do object. Now we have the to do that was post to the server added to our list, and then just send it back.
And I made a little note here about how res dot Jason and res dot sin are pretty much the same thing with some small differences with the undefined and noll. And then if we add some parameters in our euro, we can access the parameters by doing something like request dot params, which is an object of the parameters in the euros. So in this case, we have a one parameter and we have given it the name id. So if we wanted to access whatever that was, so anything after to do's, that's going to be considered the ID.
We can access it by saying request dot params, and using the name that we specified up here, which is id. So we can get the id that the person passed in and we can find our to do's and then send it back. So again, very, very simple. This is why I chose these Express. As you can see, it's powerful but yet, it's very simple. And you also can do this yourself without Express. It's a lot more work. But you can totally do it. Yeah, Mark? - Do you have to define one function for every request type? - Do you have to define one, well you need some, 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 a middle ware. - It kind of falls up. If he wants to do the same thing on both the get and the post. - Uh, hopefully, you're not doing the same thing on a get and a post. If, for whatever reason, you did do that, first of all, you wouldn't be following res, but. If you wanted to do that, then you would just, like, abstract that thing out. And either add it as a middle ware, or just call it inside this call back function. But, I think the person that is asking that question is getting into middle ware. And we'll talk more about that. So, yeah. You can, this is concepts of, like, shared function novelty 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 middle ware. So we'll see what that looks like. Oh. Next thing says middle ware, so. Express uses middle ware to modify and inspect the incoming requests. So what I mean by that is you can actually just think of this call back function as middle ware. Technically, it kind of is. So really, middleware is just a function that allows us to, that's going to be ran in an order that we register with Express. And it's job is to do whatever we want it to do.
We can use any code that it wants. But, typically, with middle ware, we're do, is like, look at the request object, inspect it, depending on what we're looking for on the request object, it will either pass the execution to the next middle ware in the stack, or it will stop the request and the response cycle right there and either throw an error or respond back to the client with like, we're done. Don't even go forward. You know, like if you don't meet these credentials, stop. You know, 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 middle ware is probably the most powerful feature in Express other than the routing.
And that's really all Express is. It's the routing and it's the middle ware. That's all it is. It's very lightweight. If you look at the source code for the Express, it's like, like one page. It's small. It's really small. It's really easy. So yeah, there are tons, because middle ware is so powerful, you could think of middle ware as like the plug-ins inside of Express. Because of that, they're such a big community of third party middle ware modules out there that we're going to be using. Express 3.0 had a lot of that stuff baked in cause they were using something called Connect.
They had a big reliance on this other framework called Connect and they got away from Connect but they wanted the same stuff. So they took out all that middle ware, and now, it's like, available, third party site. But we can still get the same stuff. So middle ware from, like, Parsing, URL's, through Haling authentication. We'll be able to use that stuff and it just makes building with Express super easy. Express can also be ecstatic web server so we serve astatic assets. And trust me, it makes it so much easier than if you did it yourself.
If you ever had to, like, write a server manually in server static assets, if you don't have any experience with that, it can be quite difficult. Like setting the appropriate mine types, reading files, dealing with buffers, stuff like that. You know, chunks. So it can be quite, you know, difficult if you don't have a lot of experience with that. Express just makes it easy. In, like, one line, it'll handle all that stuff for you.
This course was created by Frontend Masters. It was originally released on 12/30/2015. We're pleased to host this training in our library.
- Executing Node.js
- Using Express
- What is middleware?
- Testing in Node.js
- Using Mongo with Node.js
- Data modeling
- Querying data with Mongoose
- Identifying sensitive routes
- Configuring the deployment