A local farmer has hired you to design and build the routing for a website that will support their growing business: Fresh Harvest Fruits.
They already have their fruit and user data created and want you to create the routes to serve up the data based on the route.
The farmer has tasked you with completing the following to get the server and routes up and running.
users.js Router
- In
users.js, define an Express router for theusersroute. This will handle any requests to the/usersendpoint. - Get all the users in the list of users when using the
GET /usersroute. - Get a particular user in the list of users when using the
GET /users/:idroute (e.gGET /users/2returns the 2nd user). - Create a
POST /usersroute that creates a new user in the database. - Create a
PUT /users/:idroute that updates the database entry that corresponds to the provided id (e.g.PUT /users/2updates the 2nd user). - Create a
DELETE /users/:idroute that deletes the database entry that corresponds to the provided id (e.g. `DELETE users/2 deletes the 2nd user). - Export the users router, import the router to
src/app.js, and setup the the router within your main Express server. - Test your endpoints using Postman.
- In
index.test.js, create unit tests for the functionality that you constructed above.
fruits.js Router
- In
fruits.js, define an Express router for thefruitsroute. This will handle any requests to the/fruitsendpoint. - Get all the fruits in the list of fruits when using the
GET /fruitsroute. - Get a particular fruit in the list of fruits when using the
GET /fruit/:idroute (e.gGET /fruits/2returns the 2nd fruit). - Create a
POST /fruitroute that creates a new fruit in the database. - Create a
PUT /fruits/:idroute that updates the database entry that corresponds to the provided id (e.g.PUT /fruits/2updates the 2nd fruit). - Create a
DELETE /fruits/:idroute that deletes the database entry that corresponds to the provided id (e.g. `DELETE fruits/2 deletes the 2nd fruit). - Export the fruits router, import the router to
src/app.js, and setup the the router within your main Express server. - Test your endpoints using Postman.
- In
index.test.js, create unit tests for the functionality that you constructed above.
The farmer is so happy with your work! Apply server-side validation to your API to ensure that:
- You cannot create a user without a
name - You cannot create a fruit without a
color
- Run
npm installto install the necessary dependencies for this assignment. - Require the
“express-validator”package in your users router. - Import both the
checkandvalidationResultfunctions from the Express Validator package. - In the
POSTrequest when creating auser, include a parameter in between your endpoint and your callback function. This parameter should check for the “name” field in the request body. The name field in the request body should NOT BE EMPTY or be just whitespace. - Validate the result of your request object, and store that reference in a variable named
errors. - Create a condition that checks if there are errors caught in the validation result, respond with the JSON which contains:
- A key named error
- A value containing the list of errors caught
- In any other case, respond with the list of all the users including the newly added user
- In the
POSTrequest when creating afruit, include a parameter in between your endpoint and your callback function. This parameter should check for the “color” field in the request body. The color field in the request body should NOT BE EMPTY or be just whitespace. - Validate the result of your request object, and store that reference in a variable named
errors. - Create a condition that checks if there are errors caught in the validation result, respond with the JSON which contains:
- A key named error
- A value containing the list of errors caught
- In any other case, respond with the list of all the users including the newly added user
- Test your endpoints using Postman. Try to add a user without a name or a fruit without a color.
- In
index.test.js, create unit tests for the functionality that you constructed above.
- For the
POST /usersroute within the array[], include a second item that checks that the"age"in therequest.bodyis not empty and doesn’t only contain whitespace. - For the
POST /fruitsroute within the array[], include a second item that checks that the"name"in the request.body is not empty and doesn’t only contain whitespace. - Update the
PUT /users/:idandPUT fruits/:idroutes with server side validation like you did for thePOSTroutes. - Within the same
POST /users route, use Express Validator to check that the value added to the "name" field has a length between 5 and 15 characters. - Within the same
POST /fruitsroute, use Express Validator to check that the value added to the "name" field has a length between 5 and 20 characters.
