While Node.js has a built-in server module, using it to create a basic server can be overwhelming. Instead, Scott uses Express to handle server requests. Express is one of many server frameworks for Node.js which greatly simplifies the process of creating your own API.
- [Instructor] Now we're going to talk about servers and Node, and how all that stuff works. How it got here, what's the tooling for that, frameworks for that, and what we're going to be using, and then you guys will be diving right in and actually doing some stuff. So Node actually has a built-in HTTP module, so again, what I mean by that is, if I were to come inside of a Node file. I were to say, HTTP, and just log this for you so you can see what that looks like.
And I would run that. It's this huge object. This is built into Node. It's an HTTP module which allows us to create an HTTP server on a TCP connection. So that's built into Node, we didn't have to download it. It's just how Node works. Which is great, because now that allows us to build servers, but there are some limitations with it. The problem is that the configuration and the amount of code that you have to write in order to have basic features, is really really overwhelming, and at times, very difficult.
And I know this because I've built servers with pure Node, without any frameworks. It teaches you a lot. You definitely learn a lot about Node, and you're forced to do things that you, you know, there's different things you can do in Node, there's like streams and stuff, so you're forced to learn some of that stuff, but it's definitely very error-prone. Especially if you don't know much about networking. If you don't know too much about networking, you're definitely going to have some errors, maybe some memory leaks. A lot of stuff's going to be going on, so you got to kind of know what you're doing if you're using that. So there's tons of frameworks out there that exist to help us abstract that difficulty away, and one of the ones that we're going to be.
One of the most popular ones, and the one that we're going to be using, is called Express. Other notable ones include Sails.js, Hapi, Koa, Strongloop, which is actually built on Express, so is Sails, they're all built on top of Express. But Express is by far the most popular one, and in my opinion, the simplest one, but it's definitely not like, I wouldn't say it's the easiest one. But I chose this one for a very specific reason. So, Express is a framework that sits on top of Node, and uses the HTTP module to make building servers in Node not so hard.
There's still a bit of configuration with Express, so if you're expecting something like rails, Express is not it. Wow, really? (laughs) Really misspelled that. So yeah, if you're thinking like we're going to have something like rails, where there's like, rails, generate this, do this. No, it's not Express. That's why I said there are frameworks that sit on top of Express, because there's still so much configuration with Express that people have abstracted that out, and built frameworks on top of that. And then there are frameworks that sit on top of that as well, so there's a lot of abstraction, but Express, I think, is a good, a good, like, common place to start, because you don't have to deal with HTTP module directly and all the nuances of serving static content, and dealing around with URL parsing and stuff like that.
And you also don't have to, you know, you also don't get this magic stuff just happening for you, like you would with something like Sails or something like that. You still got to do some grunt work. Just a little bit though, not too much. Any questions on that? No, okay. So Express has a very welcoming API that makes getting off the ground and building an API pretty easy. In my opinion.
Really, Express, from my experience and using it a whole bunch of times, it's really just like a framework that has routing and middleware. And we'll talk about middleware. Yeah, Mark? - [Mark] There's a question about Restify, being similar to Express. Can you briefly touch on why you would use Express versus Restify? - Yeah, so, if we were talking about Express 3.0 versus Restify, I'd have used Restify. Specifically for a REST API. But with the release of Express four, and this new feature they have with sub-routers, which we'll get into later today, Express is actually pretty good at handling, or pretty good at allowing you to build a RESTful API.
Restify is purely meant for like, I want to build a RESTful API, and it's really great at that. In fact, it's way better than Express at that. But sometimes you need things like serving static assets, or maybe, you need your API server to also double as a web server, so you are serving static assets, you're doing this, you're doing that. So in that case, I would use Express, but Restify is definitely a good one if all you needed was like a RESTful API. So if you were just making an API for like a mobile application, Restify might be a good choice. I'm not sure how well it is maintained.
I know Express is maintained very very well, so I can't tell you how well the people are at maintaining Restify, but Express has been releasing pretty good, pretty heavily ever since it's been passed over to Strongloop from (mumbles), so it's doing a really good job, and it continues to keep going. But Restify is a good one as far as like API design in general. But still, none of them are like rails, you're still not going to get something like rails. The closest I've seen to anything like rails and Node is Sails.js. And even that, it's not like rails, but it's the closest.
- [Mark] And there was one more question on LoopBack versus Express. - Yeah, so LoopBack is actually a framework that sits on top of Express. LoopBack is actually the maintainers of Express as well, if you go to the Express website, right here, there's a link for LoopBack. LoopBack they maintain Express, and then they built this API framework on top of Express that just gives you a little more, it's more conventional. It does a little more stuff for you. You have a lot less set-up. So that's a good choice, but I still don't recommend it unless you even know what Express is doing.
Because when you get to LoopBack and you start messing around with like middleware and stuff, and you don't even know what middleware is, you don't know how Express is handling its stuff, you're going to be kind of confused. So I still recommend starting with Express, and then once you get familiar with that, you can branch off and do LoopBack, because a lot of the best frameworks out there on Node are built off on Express, so if you have a good solid foundation on that, then moving to something like LoopBack or Sails is going to be pretty easy for you. It's just going to be like, it's just going to come down to if you like the conventions that they're forcing you to have.
So that's what that comes down to, so it's convention over configuration, whereas Express is more configurable, and LoopBack and Sails is like more conventions. Cool, so, what was I saying. Oh yeah, so, like I said, Express has a very welcoming API. They take advantage of Node's evented i.o. Does anybody not know what I mean when I say that: evented i.o.? All right, so Node, just like java scripted browser is single threaded.
So everything is evented, it's asynchronous. There's alike an event table where things happen some point later in time, and then you use callbacks to get notified about those things. So Node has an evented i.o. the same way, and you can, you know, notify different pieces of your code about something that has come back from some time in the future. So Express takes advantage of that very very well. With the routing, and as you see, it works very very very good. So we'll see how that works, but Express allows us to register callbacks when a particular combination of HTTP verbs and routes are hit.
So that's like the beauty of Express. It's literally just like a routing library with middleware. It allows you to set up verbs with routes, and then when those routes and verbs are hit by a request, it'll run the functions you tell it to run in that order you tell it to run. And that's all Express does, that's it. So Express is just one of many server frameworks out there, for Node, like I was saying. We're using it because it's the most commonly one used, and its lack of strict conventions will force you to learn more about what is going on as we build the API.
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