A feature often comes in two halves: a page in the storefront and a route in the API. Here the product page is already written. It opens at /products/3 and asks the server for GET /api/products/3. The server has no such route yet, so the last middleware answers 404 {"error": "Not found"} and the page shows "Not found".
A route parameter is a part of the path that can change. You write it with a colon, and Express puts what it matched in req.params:
app.get("/api/greet/:name", (req, res) => {
res.json({ hello: req.params.name });
});For /api/greet/Ana, req.params.name is "Ana". To use a value like that in SQL, write a ? placeholder and pass the value to .get(…). The database then treats it as one value, never as SQL. .get gives back one row, or undefined when no row matches. For a missing row, the right answer is a 404 with a message the page can show.
Your server must answer:
GET /api/products/3 → 200 {"id": 3, "name": "Lamp", "price": 29, "stock": 1}
GET /api/products/3%20OR%201=1 → 404 {"error": "Product not found"}On the page, clicking Details for the Lamp shows its name, price and stock. Opening /products/99 shows the alert "Product not found".
Your task: in server/app.js, add GET /api/products/:id. It reads the product with a placeholder and answers it as JSON. When there is no such product, it answers 404 with {"error": "Product not found"}.
Assignment
Follow the Coding Tutorial to write the product route in server/app.js, below the products route.
Hint
Put return before the 404 answer. That stops the route there, so it doesn't also run res.json(product).
Free plan: 30 tutor messages. PRO is unlimited.
