During the second part of the second week you will be creating the API for your Video website.
Below are separate headings for each endpoint. Each of them are separated into the HTTP Request Method
type and the route that the endpoint should exist on.
For a recap on about HTTP Request Methods you can read here https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods
For example, for the first endpoint
GET
is the request method- "/" is the route
On Line 8 of server.js
you can find an example of the first endpoint that you will need to make.
Since we are using a monorepo the server and client are running on the same instance. So to run the server you should also use
npm run dev
To confirm your server is running go to
http://127.0.0.1:3000/health
in your browser. If the server is running you'll see a message saying
OK
Note: to separate the backend and frontend all backend endpoints have /api
as a prefix. So for example if you are asked to create a GET /videos
endpoint that will be available at http://127.0.0.1/3000/api/videos
Here is an example solution for the Back End:
https://video-recommendations-cyf.netlify.app/api/videos
Your code is expected to read data from the database. In order to make sure it works you will need to make sure that the backend is configured for database access. There are two steps you need to do:
- Copy the
.env.example
file over to.env
This file contains the settings for your application. Currently the only setting you need is the DATABASE_URL
setting that looks like the following:
postgres://username:password@localhost:5432/database_name
Make sure you swap the username
, password
and database_name
values with the database details that you have set up locally.
- Use your queries you created in level 200 inside the API.
The api.js
file is already set up to connect to the database if the config is done properly. To query the database you can use requests like:
const response = await db.query("SELECT * FROM videos WHERE id = $1",[id]);
Your website should have the following four standard REST endpoints to list, list, view and delete videos. The backend will also need a way to modify the rating of the video as well which endpoint you need to design yourself.
This endpoint is used to return all of the videos
See exampleresponse.json
as an example.
This endpoint is used to add a video to the API.
Both fields - title
and url
- must be included and be valid for this to succeed.
Note: When a video is added, you must attach a unique ID to so that it can later be deleted
{
"title": "The Title",
"url": "https://www.youtube.com/watch?v=ABCDEFGHIJK"
}
If successful:
{
"id": 523523
}
If not successful
{
"result": "failure",
"message": "Video could not be saved"
}
Returns the video with the ID contained within the {id}
parameter
{
"id": 1,
"title": "Never Gonna Give You Up",
"url": "https://www.youtube.com/watch?v=dQw4w9WgXcQ",
"rating": 23
}
If that ID doesn't exist in the database the endpoint should return a 404
status code with an error message, example:
{
"result": "failure",
"message": "Video not found"
}
Deletes the video with the ID container within the {id}
parameter
If successful:
{}
If that ID doesn't exist in the database the endpoint should return a 404
status code with an error message, example:
{
"result": "failure",
"message": "Video could not be deleted"
}
Your backend also needs to have a way to allow the rating of a video to be modified up and down.
Design this feature yourself based on what you have learned in the Syllabus.
Note: you don't need to adhere to the example requests and responses above, for example a response on GET /videos/{id}
like the following is also acceptable:
{
"result": "success",
"message": "Video Found",
"video": {
"id": 1,
"title": "Never Gonna Give You Up",
"url": "https://www.youtube.com/watch?v=dQw4w9WgXcQ",
"rating": 23
}
}
Design your backend's responses so it will be easy for you to link the backend and frontend applications together, including potential error scenarios.