Step 7
We are ready to build our API.
Recall the following HTTP verbs (operations):
- POST: to create a resource
- PUT: to update it
- GET: to read it
- DELETE: to delete it
We will start with HTTP GET requests.
Let's document our HTTP requests as we implement them:
Read Notes | |
---|---|
HTTP Method | GET |
API Endpoint | /api/notes |
Request Path Parameter | |
Request Query Parameter | |
Request Body | |
Response Body | JSON array of notes |
Response Status | 200 |
What is Response Status?
The response status is a "code" (number) returned to the client that signals the success/failure of their request. Here are the common status codes and their meaning:
Status | Meaning |
---|---|
200 (OK) | This is the standard response for successful HTTP requests. |
201 (CREATED) | This is the standard response for an HTTP request that resulted in an item being successfully created. |
204 (NO CONTENT) | This is the standard response for successful HTTP requests, where nothing is being returned in the response body. |
400 (BAD REQUEST) | The request cannot be processed because of bad request syntax, excessive size, or another client error. |
403 (FORBIDDEN) | The client does not have permission to access this resource. |
404 (NOT FOUND) | The resource could not be found at this time. It is possible it was deleted, or does not exist yet. |
500 (INTERNAL SERVER ERROR) | The generic answer for an unexpected failure if there is no more specific information available. |
Add this route to index.js
app.get("/api/notes", (req, res) => {
res.json(notes.readAll());
});
As you save the code, nodemon reruns the Express application. Open your browser and head over to http://localhost:4567/api/notes. You must receive a JSON array containing our 4 sample notes.
You can also hit this endpoint on Postman to test it.