Express JS Express REST API Development 1 — Questions and Answers
Question 1: Which HTTP method is conventionally used to create a new resource in a REST API built with Express?
- POST (Correct answer)
- PUT
- PATCH
- GET
Correct answer: POST
POST is the standard HTTP method for creating new resources, typically sending data in the request body.
Question 2: In a RESTful Express API, what HTTP status code should a successful resource creation return?
- 201 Created (Correct answer)
- 200 OK
- 204 No Content
- 202 Accepted
Correct answer: 201 Created
201 Created indicates that a new resource was successfully created as a result of the request.
Question 3: What HTTP method should be used for partial updates to a resource in REST?
- PATCH (Correct answer)
- PUT
- POST
- UPDATE
Correct answer: PATCH
PATCH is intended for partial modifications, while PUT replaces the entire resource representation.
Question 4: What HTTP status code should be returned when a requested resource is not found in an Express REST API?
- 404 Not Found (Correct answer)
- 400 Bad Request
- 204 No Content
- 410 Gone
Correct answer: 404 Not Found
404 Not Found is the standard response when the server cannot locate the requested resource.
Question 5: Which header should an Express API set to indicate the response body format is JSON?
- Content-Type: application/json (Correct answer)
- Accept: application/json
- X-Content-Type: json
- Response-Format: json
Correct answer: Content-Type: application/json
The Content-Type header tells the client the media type of the response body; res.json() sets this automatically.
Question 6: What does a 204 No Content response indicate in a REST API?
- The request was successful but there is no body to return (Correct answer)
- The resource was not found
- The request is still processing
- The content was deleted permanently
Correct answer: The request was successful but there is no body to return
204 No Content means the server processed the request successfully but has nothing to include in the response body.
Which HTTP method is conventionally used to create a new resource in a REST API built with Express?