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.jsfile, create aGETrequest using Express for the/restaurantsendpoint. - In the
GETrequest, return all restaurants via theRestaurant.findAll()method.- Remember to use
asyncandawait - Note that you will need to run
npm run seedonce 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.jsfile, Use Express to createGET /restaurants/:idendpoint. - In
GET /restaurants/:idget the id using thereq.paramsobject. - In
GET /restaurants/:idget 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
GETrequest 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/2would 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/2would delete the restaurant with an ID of 2.
- For example,
- Test your endpoints on Postman by making a
GET,POST,PUT, andDELETErequests 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
PUTorPOST - 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
routesfor your Express router(s) - Include a file (like
restaurants.js) within theroutesdirectory 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.jsfile.npm install supertest
- Create tests that accomplish the following:
- Verify that the
GET /restaurantsroute returns a status code of 200. - Verify that
GET /restaurantsroute returns an array of restaurants - Test that
GET /restaurantsreturns the correct number of restaurants - Test that
GET /restaurantsreturns the correct restaurant data - Verify that
GET /restaurants/:idrequest returns the correct data. - Test that
POST /restaurantsrequest returns the restaurants array has been updated with the new value. - Verify that
PUT /restaurants/:idrequest updates the restaurant array with the provided value - Test that
DELETE /restaurant/:iddeletes the restaurant with the provided id from the array.
- Verify that the
- Run
npm install express-validatorto install the Express Validator package - Include the
checkandvalidationResultmethods from the Express Validator package in your Express router for restaurants. - Navigate to your POST Request route to
/restaurantsfrom 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.bodyis not empty and doesn’t only contain whitespace - Within the array
[]include a second item that checks that the"location"in therequest.bodyis not empty and doesn’t only contain whitespace - Within the array
[]include a third item that checks that the"cuisine"is therequest.bodyis 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
errorsreference 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
Menumodel. TheMenumodel should have the following properties:title: a string
- In the models directory define an
Itemmodel. TheItemmodel 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 everyMenuhas oneRestaurant - There are also many
Item(s) included in aMenuand anItemcan be on manyMenus
- A Restaurant may have one or more
- In
seed.js:- Import the
MenuandItem. - Import the
seedMenuandseedItemdata. - Update the
syncSeedfunction to bulk create newMenuandIteminstances.
- Import the
- Navigate to your
GET /restaurantsroute. - Since you’re making a call to a database, don’t forget to use
asyncandawaitas part of the callback argument. - Use Express to load all of the restaurants from the
Restaurantmodel. - Within your Sequelize method to find all of the restaurants in the model, include several arguments
- Include the
Menus 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 /restaurantsendpoint 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.




