Building a RESTful API with MVC
Last updated
Last updated
In this lesson, we will introduce the Model layer to our server application design. At its core, a server manages data, defines API endpoints for accessing that data using CRUD operation, and provides frontend code to render a nice GUI for the user to more abstractly execute those CRUD operations.
The Model is the portion of the server application that manages the CRUD operations for a server's collection of data. For example, a web-based todo application might:
store an array of todo objects in memory
define the endponts GET /API/todos
and POST /api/todos
to get all todos and to create a new todo
provide a React application that fetches renders all the todos and a form that lets a user create a new todo.
The piece of code that sits between the endpoints and the array of todo objects is the Model.
Let's jump in!
REST API — an API that conforms to the design principles of the representational state transfer (REST) architectural style.
Model — an interface for managing a data structure. We will implement a model using a class
with static methods for performing CRUD actions on a set of data.
Postman — a tool for testing HTTP requests
Route Parameters — named URL segments that are used to capture the values specified at their position in the URL. The captured values are populated in the req.params
object
In this lesson, we will build an API that lets users manage a list of fellows. They can:
(Create) Add a new fellow to the list
(Read) Get all fellows
(Read) Get a single fellow
(Update) Change the name of a fellow
(Delete) Remove a fellow from the list
To provide a dynamic set of data that allows for full CRUD operations, we need a data layer called a model.
A model is an interface (a set of functions) for managing a data structure. We will implement a model using a class
with static methods.
Find the models/Fellow.js
file and try the following:
How will this model work within our server?
The flow of control is: Client Request > Express app
> Middleware > Controller > Model > Controller > Server Response
In the server applications we've built so far, the data and files we've sent as responses have all been hard-coded.
Now, we can use the Fellow
model to get the data we want and send it back to the client:
We have a class that can manage the list of fellows and we have controllers that can use that model. To let users execute these operations, our server needs API endpoints where a client can send requests.
For example, a GET /api/fellows
request will send back all of the fellows managed by the application.
Quiz! (Answer these with a fellow classmate)
Which action will a GET /api/fellows/5
request perform?
Which action will a PATCH /api/fellows/5
request perform?
Which action will a POST /api/fellows
request perform?
Why does the last endpoint not include a number?
https://expressjs.com/en/guide/routing.html#route-parameters
Named URL segments that are used to capture the values specified at their position in the URL. The captured values are populated in the req.params object
These API endpoints follow the REST design pattern where endpoints look like: /api/resources/:id
.
Representational state transfer (REST) is a design pattern for creating APIs that adhere to 6 pillars:
Let's zoom out and look at the organization of the server code now.
index.js
builds the app
, configures middleware, and sets the endpoints. However, the controllers are now imported.
controllers/fellowControllers.js
defines all of the controllers for endpoints relating to fellow data. Each set of data should have its own controllers file.
models/Fellow.js
defines a model for managing fellow data. This model is used exclusively by the fellow controllers. Each set of data managed by the server should have its own model.
By separating our code in this way, we show the separate "layers" of the application.
Download the Postman VS Code Extension
Create an Account on Postman
Create a collection called 810 lecture
Add a request for each of your endpoints:
GET /api/fellows
GET /api/fellows/:id
POST /api/fellows
PATCH /api/fellows/:id
DELETE /api/fellows/:id
Requests that require a body, select the body tab, then raw, and choose JSON from the type dropdown.
Then, test out your server's endpoints using postman
Build a Song
model and a server application for maintaining a playlist. Each song should have an id
, a title
, and an artist
(at minimum). The model should provide an interface for:
Creating a new song
Getting all songs
Getting a single song
Updating the title or artist of a song
Deleting a song
Then, create an endpoint and a controller for each of these pieces of functionality. The endpoints should follow REST conventions and should all begin with /api
Finally, build a frontend react application that can interact with the songs API that you've built. It should be able to:
Create: Add a new song to the list.
Read: Display a list of all songs.
Read: Display a single song.
Update: Update a single songs's title or artist.
Delete: Delete a single song.
Here is a recommended React Router page structure for your React app:
/
: The homepage which includes:
Form for creating a new song
List of all songs
/songs/:id
: The details of a single song which includes
The title, artist, and ID of the song
A form to submit a new title or artist for the song
A button to delete the song from the list