/users/42?const express = require('express'); const app = express(); app.get('/users/:id', (req, res) => { res.send(`User ID is ${req.params.id}`); }); // Assume server is listening
:id in the route path and how req.params works.The route /users/:id captures the part after /users/ as id. So when the URL is /users/42, req.params.id is 42. The response sends this value.
PUT is the standard HTTP method for updating a resource. The route /books/:id with app.put correctly matches update requests for a book by ID.
/products/123 always respond with 'All products' instead of the product ID?const express = require('express'); const app = express(); app.get('/products*', (req, res) => { res.send('All products'); }); app.get('/products/:id', (req, res) => { res.send(`Product ID: ${req.params.id}`); });
Express matches routes in the order they are defined. The first route /products* uses a wildcard that matches any path starting with /products, including /products/123. Therefore, it responds with 'All products' before the more specific /products/:id route is considered.
/orders/99?const express = require('express'); const app = express(); app.delete('/orders/:orderId', (req, res) => { const id = req.params.orderId; res.status(200).json({ message: `Order ${id} deleted` }); });
The route sends a JSON response with a message confirming deletion. The status code is 200, meaning success. The response body is a JSON string with the message.
Option A uses a clear hierarchical path showing comments belong to post 7. This is the recommended RESTful style. Option A uses a query parameter which is valid but less clear about resource hierarchy. Options C and D use verbs or confusing order, which is not recommended.