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
app.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()
). - Start your server with
node server.js
. - Test your endpoint by visiting http://localhost:3000/restaurants. Your response should look similar to the one shown below:
- In your
app.js
file, Use Express to createGET /restaurants/:id
endpoint. - In
GET /restaurants/:id
get the id using thereq.params
object. - In
GET /restaurants/:id
get the particular restaurant via the methodfindByPk()
. - Send the found restaurant 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 tohttp://localhost:3000/restaurants/1
. Your browser should output the following on Postman:
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 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.
- For example,
- 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.
- For example,
- Test your endpoints on Postman by making a
GET
,POST
,PUT
, andDELETE
requests to http://localhost:3000/restaurants/
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/restaurants/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.
- Create a new directory called
routes
for your Express router(s) - Include a file (like
restaurants.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 Restaurants database.
- Export your restaurants router
- Include a reference to your router in your
app.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
package.json
, update the test script to be"test": "jest --watchAll"
. - In the root directory, create an
index.test.js
file.npm install supertest
- Create tests that accomplish the following:
- Verify that the
GET /restaurants
route returns a status code of 200. - Verify that
GET /restaurants
route returns an array of restaurants - Test that
GET /restaurants
returns the correct number of restaurants - Test that
GET /restaurants
returns the correct restaurant data - Verify that
GET /restaurants/:id
request returns the correct data. - Test that
POST /restaurants
request returns the restaurants array has been updated with the new value. - Verify that
PUT /restaurants/:id
request updates the restaurant array with the provided value - Test that
DELETE /restaurant/:id
deletes the restaurant with the provided id from the array.
- Verify that the
- 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 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"
, and/or"cuisine"
fields. - In index.test.js, create unit tests that test that an errors array is returned when the
"name"
,"location"
, and/or"cuisine"
fields are empty
TASK: Create a new Item
and Menu
model and define the associations between the three models. Update the GET /restaurants route to GET a list of all the Restaurants in the Restaurant database, including the Menu
(s) that belong to that restaurant, and including the Item
(s) that belong to that Menu
.
- In the models directory define a
Menu
model. TheMenu
model should have the following properties:title
: a string
- In the models directory define an
Item
model. TheItem
model should have the following properties:name
: a stringimage
: a stringprice
: a numbervegetarian
: a boolean
- Export the models and import into
models/index.js
- In
models/index.js
, define the following association:- A Restaurant may have one or more
Menu
(s), but everyMenu
has oneRestaurant
- There are also many
Item
(s) included in aMenu
and anItem
can be on manyMenu
s
- A Restaurant may have one or more
- In
seed.js
:- Import the
Menu
andItem
. - Import the
seedMenu
andseedItem
data. - Update the
syncSeed
function to bulk create newMenu
andItem
instances.
- Import the
- Navigate to your
GET /restaurants
route. - Since you’re making a call to a database, don’t forget to use
async
andawait
as part of the callback argument. - Use Express to load all of the restaurants from the
Restaurant
model. - Within your Sequelize method to find all of the restaurants in the model, include several arguments
- Include the
Menu
s as part of the response
{include: Mode1} //Argument 1
- Include from the menu, the items in that menu
{ include: Mode1, //Which model should we add here? include: [{ model: Mode1, include: [{ model: Model2 //Which model should we add here? }] }] } //Argument 2
- Include the
- Test your
GET /restaurants
endpoint using Postman.
Bonus Assignment: Within the same POST /restaurant route above, use Express Validator to check that the value added to the "name" field on a restaurant has a length between 10 and 30 characters.
- You may use this reference 🔍 to help you find the correct method for your solution.
- Include a fourth item within your array
[]
that checks that the"name"
field has a length between 10 and 30. (Minimum 10, Maximum 30) - Use this reference to locate the best method for checking the length of the value passed into the restaurant’s “name” field. Look up how to specify a range within the function you find.
- When you find the appropriate method, make sure to include an argument to indicate that the minimum length should be 10 characters, and the maximum length should be 30 characters.
- Test using Postman. Try to add a restaurant
"name"
with less than 10 characters, or more than 30 characters. - In
index.test.js
, create a unit test that tests that someone can only add a restaurant name bigger than 10 characters and smaller than 30 characters.