API Design in Node.js, v3 API Design in Node.js, v3

CRUD Controller Design Overview

Check out a free preview of the full API Design in Node.js, v3 course:
The "CRUD Controller Design Overview" Lesson is part of the full, API Design in Node.js, v3 course featured in this preview video. Here's what you'd learn in this lesson:

Scott gives background on controller design, and pseudocodes route combinations in preparation for the next exercise.

Get Unlimited Access Now

Transcript from the "CRUD Controller Design Overview" Lesson

>> Scott Moss: So yeah, so now that we have the CRUD, we know a route combination adheres to some type of CRUD, right? So if I were just to map those out very quick, I'm just gonna write some pseudocode here. If I were to map those out, I know a GET request to a /, that would be a create.

[00:00:19] I know a GET request to a / id would also be a create, I'm sorry, not create. That would be a read. A GET request to a / id would also be a read, but it would be a read one. So I'll just put that.
>> Scott Moss: This is like read many.

[00:00:39] If I did a POST request to a /, that's gonna be a create one. If I did a PUT request to a / id, that's gonna be an update one. And if I did a DELETE request to a / id, that's gonna be a delete one.
>> Scott Moss: So knowing all that, if we already made the routes that already create this routing logic, then all we gotta do is add these queries inside of our controllers that change that.

[00:01:16] So for the create, for the GET one, which is read many, then I would come over here and say, okay, for read, we have a model.find. That's gonna read many. So I will use that one inside of that controller. For a read one, I would use model.findOne or findById.

[00:01:35] In this case, we have an id in the URL, so I would use that id in the URL. So that's how we get that one. So that's how all that stuff works. But because we're gonna generalize them, what you're gonna do is you're gonna go down into, there's a file here called crud.js.

[00:01:51] And inside of there, that's where we're gonna generalize these controllers to work for all of the resources that are there. And then you won't have to create one for every single one. So that's what we're gonna do in the next exercise.