MEAN RESTful API Design with Node.js and Express — Questions and Answers
Question 1: According to REST principles, which HTTP method should be used for a partial update of a resource (modifying only some fields)?
- PUT
- POST
- PATCH (Correct answer)
- UPDATE
Correct answer: PATCH
PATCH is used for partial updates — only the provided fields are changed. PUT replaces the entire resource, meaning omitted fields would be deleted or set to defaults. POST creates new resources. UPDATE is not a valid HTTP method.
Question 2: Which HTTP status code should a RESTful Express API return when a resource is successfully created via a POST request?
- 200 OK
- 201 Created (Correct answer)
- 204 No Content
- 202 Accepted
Correct answer: 201 Created
201 Created is the correct status for a successful POST that results in a new resource. The response should also include a Location header pointing to the new resource's URL. 200 OK is for successful GET/PUT/PATCH; 204 is for successful DELETE with no response body.
Question 3: What is the recommended approach for versioning a REST API in an Express application?
- Include the version in the URL path, e.g., `/api/v1/users` (Correct answer)
- Pass the version as a query parameter, e.g., `?version=1`
- Use a custom HTTP method like GETV1
- Versioning is unnecessary if you use semantic versioning in package.json
Correct answer: Include the version in the URL path, e.g., `/api/v1/users`
URL path versioning (/api/v1/, /api/v2/) is the most widely adopted approach because it is explicit, easy to route in Express with router.use('/v1', v1Router), easy to test, and visible in logs and browser history. Header-based versioning is an alternative but less discoverable.
Question 4: In a RESTful API, what should the response body contain when returning HTTP 204 No Content?
- An empty body — no content should be sent (Correct answer)
- A JSON object confirming the operation succeeded
- The deleted resource's data for client-side cache invalidation
- An error message explaining why there is no content
Correct answer: An empty body — no content should be sent
204 No Content explicitly means the server successfully processed the request but has no content to return. Sending a body with a 204 response violates the HTTP spec and may be ignored or cause errors in some clients. It is commonly used for successful DELETE operations.
Question 5: When designing a REST API endpoint to retrieve all orders for a specific user, which URL structure best follows REST conventions?
- /api/getUserOrders?id=123
- /api/users/123/orders (Correct answer)
- /api/orders/getByUser/123
- /api/fetchOrders?userId=123
Correct answer: /api/users/123/orders
REST favors noun-based hierarchical resource paths rather than verb-based URLs. /users/123/orders clearly expresses the relationship: orders belonging to user 123. Verb-based paths like getUserOrders or getByUser are RPC-style, not RESTful.
Question 6: Which Express.js pattern correctly sends a JSON error response with an appropriate status code when a requested resource is not found?
- res.send(404, { error: 'Not found' })
- res.status(404).json({ error: 'Not found' }) (Correct answer)
- res.json({ status: 404, error: 'Not found' })
- res.error(404, 'Not found')
Correct answer: res.status(404).json({ error: 'Not found' })
The correct Express pattern chains .status() to set the HTTP status code and .json() to serialize and send the response body with the correct Content-Type: application/json header. The two-argument form of res.send() was deprecated and removed in Express 4.
According to REST principles, which HTTP method should be used for a partial update of a resource (modifying only some fields)?