5.1. Pet Adoption App
Objective
- Build a fullstack app that displays a list of pets available for adoption and allows users to submit new ones.
- Use React Router for navigation between different pages in the frontend.
- Serve the React frontend from Express in production.
- Include one server-rendered page using EJS at
/adminto display a full pet list for administrators.
Features
Pet API (Express)
- Build an API with the following routes:
GET /api/pets: return all petsPOST /api/pets: add a new pet
- Use an in-memory array to store pet data.
- Each pet should have a
name,type, andage.
- Build an API with the following routes:
React Frontend (with React Router)
- Use
react-router-domto define these routes:/: Homepage with a welcome message/pets: Display the list of pets/add: A form to add a new pet
- Navigation between routes should be seamless and handled by the frontend.
- Each route should fetch or update data as needed from the backend API.
- Use
Proxy in Development
- Use the
proxyfield inpackage.jsonto forward/apirequests to Express during development.
- Use the
Serving React from Express in Production
- Run
npm run buildto generate the production version of the React app. - Use
express.static()to serve thebuilddirectory. - Use
app.get("*")to allow React Router to handle routing on the frontend.
- Run
Server-Rendered Admin Page
- Add a route at
/adminthat renders an EJS page. - This page should:
- Display the total number of pets
- List each pet with name, type, and age
- Add a route at
Stretch Features
Pet Detail View
- Add
/pets/:idas a dynamic route in React. - Use a
GET /api/pets/:idroute on the backend to return a single pet.
- Add
Delete Pet
- Add a
DELETE /api/pets/:idendpoint. - Show a delete button next to each pet on the admin page.
- Add a
Form Validation
- Ensure that all form fields are required.
- Add basic validation in React (e.g. positive age, text inputs not empty).
Adoption Toggle
- Add a button to toggle a pet’s
adoptedstatus. - Include a
PUT /api/pets/:idendpoint to support it.
- Add a button to toggle a pet’s