Over the next series of lessons we will be building an application using Express that will be able to GET
, POST
, PUT
, and DELETE
values stored in a database.
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 aGET
request using Express for the/restaurants
endpoint - In the
GET
request, return all restaurants via theRestaurant.findAll()
method.- Remember to use
async
andawait
- Note that you will need to run
npm run seed
once in order to put data into the restaurant database.
- Remember to use
- Send the restaurants as a JSON Response (
res.json()
) - Test your endpoint by visiting http://localhost:3000/restaurants. Your response should look similar to the one shown below:
- In your
server.js
file, use express to handle aGET
request to the endpoint"/restaurants"
include a URL Parameter to the endpoint“restaurants”
named“id”
. - In your
“/restaurants”
with the URL parameter id route handler get the particular restaurant via the methodRestaurant.findByPk()
. - The endpoint will need to fetch the particular restaurant based on the value of your route parameter from the database - have a look at the Sequelize Models
findByPk()
method to help you with this. - Send the found restaurant as a JSON response (
res.json()
). - Test your endpoint using Postman by sending a
GET
request tohttp://localhost:3000/restaurants/1
. Your browser should output the following on Postman:
- Include middleware to parse data included in the body of your request as JSON and URL Encoded. You can use either for this activity.
- Create an express route for creating (adding) a new restaurant on your restaurant database.
- Create an express route for updating (replacing) an existing restaurant with a new restaurant on your restaurant database based on ID in the route. For example,
restaurant/2
would update the restaurant with an ID of 2. - Create an express route for deleting (removing) a restaurant on your database based on the id in the route. For example,
restaurant/2
would delete the restaurant with an ID of 2. - Test your “Main Assignment” endpoint on Postman by making a
GET
,POST
,PUT
, andDELETE
requests to http://localhost:3000/restaurants/
- Create a new directory for your express route(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 Restaurants 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
- Run
npm install express-validator
to install the Express Validator package - Include the check and
validationResult
methods from the Express Validator package in your Express Router for restaurants. - Navigate to your POST Request route to
/restaurants
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 “location” in therequest.body
is not empty and doesn’t only contain whitespace - Within the array
[]
include a third item that checks that the “cuisine” is 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 key error and the value
errors.array()
- If the
errors
reference is empty (there are no errors), then continue with adding the restaurant to the Restaurant DB and return a list of all the restaurants including the newly added one. - Test your endpoint using Postman. Check to see if you can add a restaurant without any of the “name”, “location” or “cuisine” fields.