Learning Outcomes
- describe the essential features of a RESTful API
- utilize the Feathers framework to implement a RESTful API for an application
- incorporate a database (MongoDB) into a web application
Resources
- Understanding and Using REST APIs
- Best Practices for Designing a Pragmatic RESTful API
- MongoDB
- mongoose
- Feathers
- slides
Lab
Video walk through of this lab.
Fork this project: https://gitlab.com/langarabrian/rest2
.
Remove the fork relationship, make the project private, and clone YOUR COPY of the project into your Cloud Shell environment.
Change to the project directory (e.g. cd rest2
).
Install the dependencies and start the application:
Preview the running application and verify that the signup page appears in the browser.
Stop the application with Ctrl-C.
Create REST Service Backend
Install the feathers CLI:
1
npm i -g @feathersjs/cli
Create a new service for the application to manage “signups”:
Answer the questions as follows:
1
2
3
4
What kind of service is it? Mongoose
What is the name of the service? signups
Which path should the service be registered on? /signups
What is the database connection string? MONGODBURI
MONGODBURI
is an environment variable containing the MongoDB connection string. You will have to set this before running the application, e.g.
1
export MONGODBURI='mongodb+srv://...'
The generated file, src/mongoose.js
, is missing a connection setting. Add useUnifiedTopology: true
to the list of mongodb connection settings.
In your editor, open up src/models/signups.model.ts
.
There is currently a Mongoose schema definition for signups that looks something like this:
Edit it so that it looks like this:
Create REST Client Frontend
In public/index.html
add the following lines toward the end of the file after bootstrap.min.js
but before form-validation.js
:
In public/form-validation.js
add the following code at the beginning of the load
event listener to set up the signups
service in the client:
A little further down in public/form-validation.js
there is code to validate the form fields and nothing else:
Modify it as follows to use the signups
service to create a new signup when the form is valid:
Re-start the backend:
Preview the applicaition and load in its own browser tab.
Fill out the form and submit.
You can verify that the data got added to the MongoDB database by looking at the collection in the MongoDB Atlas console.
Read Data from REST Service
Next, we will modify the client-side app so that it displays all the current signups in the table at the bottom of the page.
First, at the top of public/form_validation.js
add a function at the very top of the file that adds a single row to the table:
Then, add a function that will fetch the all of the signups from the server and then add them one at a time to the table:
Finally, call the showSignups
function on page load. In the load
event listener, add the following code just after the connection to the signups
service has been established.
Assignment
- Add the required code to
public/form_validation.js
to immediately add the form data to the table at the bottom of the page. - Add the required code to
public/form_validation.js
andpublic/index.html
to display a “Delete” button at the end of each row in the table. When the “Delete” button is clicked, the corresponding row should be deleted from the table and you should invoke the correct Feathers service method to delete the document from the MongoDB collection. - Implement CI/CD (
.gitlab-ci.yml
) for the project so that the application will be deployed to Google Cloud Run. - Stage, commit, and push the changes.