Transcript from the "Use Express to Build a REST API" Lesson
>> Scott Moss: What we're gonna do now is, at this point, you've pretty much learned a lot about Mongo, and Mongoose, and using it in Node. And I could promise you, you know enough to build and app in Mongo and Mongoose, you can get to that point. If it starts crapping the bed because it's not scaling, that's a whole other thing.
[00:00:22] But you can definitely get to the point where this thing can host over a million people, easily, with what we just learned. So with that in mind, the next thing I wanna do is show you how to actually use Mongo in an app. We've been doing all of these tests and all these isolated queries and creations and getting those things to pass.
[00:00:38] It's really cool, you're just be like, okay, well, how do I actually use this inside of an app, how does this work? And you'd be surprised, it's the same thing, nothing really changes. So what I wanna do is, I wanna to spend just a little bit of time of hooking up a basic Hello World app using Express, and showing you how Mongo could tie into it.
[00:00:59] It could tie into it in so many different ways, we're just showing you one way of how that would work. And then just set you loose and have you fix some of that on your own, sound good? All right, so what we're gonna do is, I'm just gonna come in here to test.js again.
[00:01:11] I'm just gonna blow all this out, since we don't really need it.
>> Scott Moss: All right, so, well, we're gonna create a small server. I'm not gonna talk too much about the implementations of what I'm doing. It's gonna be small, we're gonna do it all in one file. But what's important, is we're gonna focus on the controllers of a server.
[00:01:33] And that's where the database stuff is gonna happen. So that's where you're gonna be looking into what's happening with Mongo and Mongoose, is the controllers. In the exercises, the only thing you're focused on is the controllers. So you don't have to really worry about the specifics of this.
[00:01:44] This is just a practical example of how you actually use this database in an application today to get started. So I'm just gonna use a package called express, which is a very good package, I use it all the time in Node to create a server.
>> Scott Moss: And express is pretty simple, if you've used express before, you know what I'm doing.
[00:02:04] If you don't know what I'm doing, I'm just gonna be talking about it as I go, but we're not gonna be diving deep into wxpress. We have a course for that already at FrontendMasters. Check that one out if you don't know about express, and a little more deeper dive into it.
[00:02:16] But basically, all I'm gonna do is I'm just going to create some routes that I can hit from an API. And then those routes are just gonna run database operations when they're activated. That's it, that's all express is gonna help me do. So the first thing I need to do is just set up some middleware.
[00:02:33] Again, you won't have to do any of this in your exercise. And middleware is very similar to what you've been doing in Mongo itself. So this middleware is to just help us debug some things.
>> Scott Moss: So again, if you wanna follow along, feel free, if you already know express, this is probably not that big of a deal.
>> Scott Moss: body-parser.
>> Scott Moss: Okay, so let's set up some logging, so we can see some logging here. And make sure we can actually post some stuff here.
>> Scott Moss: And then,
>> Scott Moss: Make sure this works.
>> Scott Moss: Okay, so now that I've got the middleware set up, now I'm just gonna add some routes.
[00:03:33] So let's say we have an API server that gets notes, it's a note-taking app. And we wanna be able to get our notes, and we wanna be able to create notes. So how would you do that in something like Node or express, it's pretty simple. So in your server framework of choice, we're using express, you would just define a route.
[00:03:52] So in our case, we're gonna say, I wanna do a get request to, let's just say /notes, so that would give me all my notes. And then from there, I'm just gonna run a function. This function is literally everything we've been doing all day today. This is where you're gonna do your database stuff.
[00:04:09] The only difference is, after you do your database stuff, you have to respond to the request, that's it. So inside of here, this is where you would use your Mongo stuff. So how you get in here is totally up to you. Most of the time, you'll have your models in another file.
[00:04:24] We're gonna do it in one file now, but we'll have them in another file, which is like we do in all of our exercises. And it will just require them, right, then you'll start using them in here. Some people put their controllers, this functions are called controllers, inside of their own file that use models.
[00:04:37] So there's many different patterns, but for this example, just put everything in one file. So I'm just gonna make a new model right now, I'm gonna call it a note. So I'm gonna say, I'm gonna make a noteSchema = new mongooseSchema.
>> Scott Moss: And a note just has a title, that's a String, and it's required.
>> Scott Moss: And it's unique.
>> Scott Moss: And then it has a body.
>> Scott Moss: Super simple note, this type is a String.
>> Scott Moss: And we can put some validation like minlength, minlength of 10.
>> Scott Moss: So that's our noteSchema, now you can make a Note model. So I'll just call this Note = mongoose.model('notes', noteSchema).
[00:05:36] Cool, so now we have our schema, we have our model, we can start using this in our API. So I wanna be able to get all the notes, so first thing I'll do is, I'll query the database for all the notes. So I'll make this async and I'll just say, notes equals await Note.find,
>> Scott Moss: All of them, cool, so I got all the notes. And then the rest is just framework specific stuff. So for express, I set the status code to 200, and then I send back some JSON as notes, and then there we go. That's it, that's how you would create an API server that grabs all the notes from a database using something like express.
[00:06:20] Pretty simple, I mean, this controller's literally two lines of code. Yeah, in the real application, you would have way more logic, like checking permissions. And you probably wouldn't check the whole collection of notes and send them back, it'd be based off some user ID or something like that.
[00:06:35] But you can see, it's pretty simple.