Over the next series of lessons we will be building an application using Express that will be able to GET
, POST
, PUT
, and DELETE
musicians from Musicians DB
After forking and cloning the repository, run the following (npm run seed
runs the seed file):
npm install
npm run seed
npm start
- Create a
GET /musicians
route for app withreq
andres
parameters. - Get all musicians via the
Musician.findAll()
method within the route.- Remember to use
async
andawait
- Note that you only need to run
npm run seed
once in order to put data into the musician database.
- Remember to use
- Send the musicians as a JSON Response (
response.json()
). - Start your server with node server.js.
- Test your endpoint by visiting http://localhost:3000/musicians. Your browser should output the following:
- In
index.test.js
, create tests to verify that yourGET /musicians
endpoint is returning the desired information. A few useful tips to aid your unit test creation:- The response to an endpoint can be accessed using the
supertest
package. You will need to require this in your testing file.
const request = require("supertest");
- You can send a request using the
.get()
method
test("Testing bakedGoods endpoint", async () => { // Sends request to `/bakedGoods` endpoint const response = await request(app).get("/bakedGoods"); })
- A HTTP status code of 200 indicates a successful GET request was made. We can access a status code using the
response.statusCode
method.
test("Testing bakedGoods endpoint", async () => { // Sends request to `/bakedGoods` endpoint const response = await request(app).get("/bakedGoods"); expect(response.statusCode).toBe(200); })
- When sending data from a database the response sends a JSON string. To convert this back to a JSON object where we can access values, we can use
JSON.parse()
on the text in the response.
test("Testing bakedGoods endpoint", async () => { // Sends request to `/bakedGoods` endpoint const response = await request(app).get("/bakedGoods"); const responseData = JSON.parse(response.text); // Write expect tests here })
- The response to an endpoint can be accessed using the
In src/app.js
:
- Use Express to create
GET /musicians/:id
endpoint. - In
GET /musicians/:id
get the id using thereq.params
object. - In
GET /musicians/:id
get the particular musician via the methodfindByPk()
. - Send the found musician as a JSON response (
res.json()
). - Start your server with
node server.js
- Test your endpoint using Postman or your browser by sending a GET request to http://localhost:3000/musicians/1. Your browser should output the following on Postman:
- In
index.test.js
, create tests for this new endpoint.
In src/app.js
:
- Call
app.use()
and pass itexpress.json()
so that we can parse the request body that contain JSON objects. - Call
app.use()
and pass itexpress.urlencoded()
so that we can parse the request body with urlencoded values. - Create an Express route for creating (adding) a new musician on your musician database.
- Create an express route for updating (replacing) an existing musician with a new musician in your musician database based on ID in the route.
- For example,
PUT musicians/2
would update the musician with an ID of 2.
- For example,
- Create an express route for deleting (removing) a musician in your database based on the id in the route.
- For example,
POST musicians/2
would delete the musician with an ID of 2.
- For example,
- Test your endpoints on Postman by making a
GET
,POST
,PUT
, andDELETE
requests to http://localhost:3000/musicians/ - In
index.test.js
, create tests for this new endpoint.
DELETE
DELETE
requests typically do not have a request body. To send these requests in Postman:
- Start the server using node server.js.
- Copy the URL (something like https://localhost:3000/musician/1) into Postman.
- Set the method to
DELETE
- Send the request.
- When you refresh the URL, you will see the value has been deleted.
PUT
and POST
Creating and updating values with POST
and PUT
requests requires that we send information in the body of the HTTP request. To send these requests in Postman:
- Set the method to
PUT
orPOST
- In Postman, select Body and then "raw".
- Paste the object into the body and ensure it is formatted correctly (i.e. JSON key values need to be in quotes).
- Send the request
- Refresh the page to see the updated array of values.
- In
index.test.js
, update the tests to reflect the functionality you created above.
- Create a new directory called
routes
for your Express router(s) - Include a file (like
musicians.js
) within theroutes
directory to represent your Express router - Define your Express router to be able to handle creating, reading, updating, and deleting resources from your
Musician
database. - Export your musicians router
- Include a reference to your router in your main express server in
server.js
- Use the Express router in your main server
- Remove any pre-defined routes from your main server and use only your Express router.
- Test your endpoints using Postman
- In
index.test.js
, create unit tests to test the functionality that you created above.
- Run
npm install express-validator
to install the Express Validator package - Include the
check
andvalidationResult
methods from the Express Validator package in your Express Router for musicians. - Navigate to your
POST
Request route to/musicians
from your Express router and include a parameter[]
in between the endpoint and the callback function. - Within the array
[]
include a first item which checks that the“name”
field in therequest.body
is not empty and doesn’t only contain whitespace. - Within the array
[]
include a second item that checks that the“instrument”
in therequest.body
is not empty and doesn’t only contain whitespace. - Within the callback function, validate the results of your checks and store them in a variable named
errors
. - Check that if the
errors
reference is not empty (there are errors), respond with a JSON that contains the keyerror
and the valueerrors.array()
. - If the
errors
reference is empty (there are no errors), then continue with adding the musician to theMusicians
DB and return a list of all the musicians including the newly added one. - Test your endpoint using Postman.
- Check to see if you can add a musician without a value in the
“name”
field. - Check to see if you can add a musician without a value in the
“instrument”
field.
- Check to see if you can add a musician without a value in the
- In
index.test.js
, create unit tests that test that an errors array is returned when the"name"
and/or"instrument"
fields are empty.
In addition to a Musician
model, there is a Band
model that is associated by the following relationship:
- Every Musician has one
Band
, but aBand
can have manyMusician
instances.
- In models/index.js, define the following association:
- Every
Musician
has one Band, but aBand
can have manyMusician
instances.
- Every
- In your routes directory, define a
bands.js
route. - Create a
GET /bands
endpoint that returns all theBand
instances including theMusician
instances that are part of thatBand
. - Create a
GET /bands/:id
endpoint that returns theBand
based on the ID passed into the request, and include theMusician
(s) that are part of that particularBand
. - Export your bands router.
- Include a reference to your router in your main express server in
app.js
. - Use the Express router in your main server
- Test your route with Postman
- In
index.test.js
, create unit tests to test the functionality that you created above.
- Include the third item within your array [] that checks that the
"name"
field has a length between 2 and 20. (Minimum 2, Maximum 20). Use this reference to locate the best method for checking the length of the value passed into the musician’s"name"
field. - Include the fourth item within your array
[]
that checks that the"instrument"
field has a length between 2 and 20. (Minimum 2, Maximum 20). - Update the
PUT /musicians/:id
route with server side validation like you did for thePOST
route. - In
index.test.js
, create tests that test the new functionality you created.