Node.js Express.js & REST APIs — Questions and Answers
Question 1: What is middleware in Express.js?
- Functions that have access to the request, response, and next function in the application's request-response cycle (Correct answer)
- Software installed between the client browser and the server hardware
- A database layer between the application and storage
- CSS libraries that sit between HTML and JavaScript
Correct answer: Functions that have access to the request, response, and next function in the application's request-response cycle
Express middleware functions execute during the request-response cycle, accessing the request object, response object, and calling next() to pass control to the next middleware.
Question 2: What HTTP method should be used to update an existing resource in a REST API?
- PUT or PATCH (Correct answer)
- GET
- POST
- DELETE
Correct answer: PUT or PATCH
PUT replaces the entire resource with the provided data, while PATCH applies partial modifications. Both are appropriate for updating existing resources in RESTful API design.
Question 3: What is the purpose of 'app.use()' in Express.js?
- To mount middleware functions that will execute for every request matching the specified path (Correct answer)
- To declare the application will use a specific database
- To import utility functions from npm packages
- To define the application's user interface layout
Correct answer: To mount middleware functions that will execute for every request matching the specified path
app.use() mounts middleware functions at a specified path (or all paths if none specified), executing them for every incoming request that matches.
Question 4: What is CORS and how is it handled in Express?
- Cross-Origin Resource Sharing — a security mechanism configured via the 'cors' middleware package (Correct answer)
- A database connection pooling library
- A caching strategy for reducing server load
- A CSS rendering optimization for server-side rendering
Correct answer: Cross-Origin Resource Sharing — a security mechanism configured via the 'cors' middleware package
CORS is a browser security mechanism that restricts cross-origin HTTP requests. In Express, the 'cors' middleware package configures appropriate headers to allow or restrict cross-origin access.
Question 5: What status code should a REST API return when a resource is successfully created?
- 201 Created (Correct answer)
- 200 OK
- 204 No Content
- 301 Moved Permanently
Correct answer: 201 Created
HTTP 201 Created indicates that the request succeeded and a new resource was created. The response typically includes the created resource or a Location header pointing to it.
Question 6: What is the purpose of route parameters in Express?
- To capture dynamic values from the URL path for use in request handlers (Correct answer)
- To set query parameters in the response
- To define the maximum number of routes allowed
- To configure routing table priorities
Correct answer: To capture dynamic values from the URL path for use in request handlers
Route parameters (e.g., '/users/:id') capture dynamic segments from the URL path, making them available via req.params for use in request handlers.
What is middleware in Express.js?