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
- In the
server.js
file, create a new endpoint for when the browser makes aGET
request to http://localhost:3000/musicians. - The endpoint will need to fetch these musicians from the database. Have a look at the Sequelize Model's
findAll()
method from last week to help you with this. - Send the musicians as a JSON Response (
response.json()
). - Test your endpoint by visiting http://localhost:3000/musicians. Your browser should output the following:
- In the
server.js
file, create an expressGET
request to an endpoint for/musicians
. - Include a route parameter of id as part of your
/musicians
endpoint. - The endpoint will need to fetch a musician from the database, based on the Router Parameters value - have a look at the Sequelize Model's
findByPk()
method to help you with this. - Create a response as a JSON object for the referenced musician object that was found in the database.
- Test your endpoint using Postman: http://localhost:3000/musicians/1. Your browser should output the following:
- In the
server.js
file, include necessary middleware for parsing data provided in the request of the body. - Create an express route for creating (adding) a new restaurant on your musician database. For example,
/musicians
via aPOST
request would create a new database entry based on what is contained in the HTTP request body. - Create an express route for updating (replacing) an existing musician with a new musician on your musician database based on ID. For example,
/musicans/2
would update the musician with an ID of 2 with the content in the HTTP request body. - Create an express route for deleting (removing) a musician on your database. For example,
musicians/2
would delete the musician with an ID of 2 from the database. - Test your endpoint using Postman and make sure to use the correct HTTP Verb when making your requests to your server.
- Create a new directory for your express router(s).
- Include a file within that directory to represent your express router
- Define your express router to be able to handle creating, reading, updating, and deleting resources from your
Musicians
database - Export your router
- Include a reference to your router in your main express server
- 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. Make sure to test a
GET
,POST
,PUT
, andDELETE
endpoint.
- 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.