Recall & Review
beginner
What is the purpose of setting response headers in Node.js?
Response headers provide metadata about the response, such as content type, caching rules, and security policies. They help the browser understand how to handle the response.
Click to reveal answer
beginner
How do you set a single response header in Node.js using the http module?
Use the response object's setHeader method, like:
res.setHeader('Content-Type', 'application/json').Click to reveal answer
intermediate
What happens if you try to set a header after the response has been sent?
Node.js will throw an error because headers must be set before sending the response body. Once headers are sent, they cannot be changed.
Click to reveal answer
intermediate
How can you set multiple headers at once in Node.js?
You can use
res.writeHead(statusCode, headersObject) to set status and multiple headers together, for example: res.writeHead(200, {'Content-Type': 'text/html', 'Cache-Control': 'no-cache'}).Click to reveal answer
beginner
Why is it important to set the 'Content-Type' header correctly?
It tells the browser what kind of data is being sent (like HTML, JSON, or plain text), so the browser can display or process it properly.
Click to reveal answer
Which method sets a single response header in Node.js?
✗ Incorrect
The correct method to set a single header is res.setHeader().
When must response headers be set in Node.js?
✗ Incorrect
Headers must be set before sending the response body because they are sent first.
What does the 'Content-Type' header specify?
✗ Incorrect
Content-Type tells the client what kind of data is in the response.
Which method sets multiple headers and status code at once?
✗ Incorrect
res.writeHead() sets the status code and multiple headers together.
What happens if you set a header after calling res.end()?
✗ Incorrect
Headers must be set before res.end(); otherwise, Node.js throws an error.
Explain how to set response headers in Node.js and why it is important.
Think about how the browser knows what kind of data it receives.
You got /4 concepts.
Describe the difference between res.setHeader() and res.writeHead() in Node.js.
One is for single headers, the other for multiple headers and status.
You got /4 concepts.