Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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?
Ares.headerSet()
Bres.getHeader()
Cres.sendHeader()
Dres.setHeader()
✗ Incorrect
The correct method to set a single header is res.setHeader().
When must response headers be set in Node.js?
AAfter sending the response body
BBefore sending the response body
CAny time during the request
DOnly after the response ends
✗ Incorrect
Headers must be set before sending the response body because they are sent first.
What does the 'Content-Type' header specify?
AThe type of data in the response
BThe size of the response
CThe server's IP address
DThe client's browser version
✗ Incorrect
Content-Type tells the client what kind of data is in the response.
Which method sets multiple headers and status code at once?
Ares.writeHead()
Bres.setHeaders()
Cres.sendHeaders()
Dres.setStatus()
✗ Incorrect
res.writeHead() sets the status code and multiple headers together.
What happens if you set a header after calling res.end()?
AThe response restarts
BThe header is set successfully
CAn error occurs because headers are already sent
DThe header is ignored silently
✗ 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.
Practice
(1/5)
1. What is the purpose of setting response headers in a Node.js server?
easy
A. To execute JavaScript code on the server
B. To provide the browser with important information about the response
C. To store data permanently on the server
D. To create new files on the server
Solution
Step 1: Understand what response headers do
Response headers tell the browser details about the response, like content type or caching rules.
Step 2: Identify the correct purpose
Only To provide the browser with important information about the response describes this role correctly; other options describe unrelated server tasks.
Final Answer:
To provide the browser with important information about the response -> Option B
Quick Check:
Response headers = browser info [OK]
Hint: Headers tell browser about response before body [OK]
Common Mistakes:
Thinking headers run code on server
Confusing headers with server storage
Believing headers create files
2. Which of the following is the correct syntax to set a response header named Content-Type to application/json in Node.js?
easy
A. response.setHeader('Content-Type', 'application/json');
B. response.header('Content-Type', 'application/json');
C. response.set('Content-Type', 'application/json');
D. response.addHeader('Content-Type', 'application/json');
Solution
Step 1: Recall Node.js method for setting headers
Node.js uses response.setHeader(name, value) to set headers.
Step 2: Match syntax with options
Only response.setHeader('Content-Type', 'application/json'); uses setHeader correctly; others use incorrect method names.
Final Answer:
response.setHeader('Content-Type', 'application/json'); -> Option A
Quick Check:
Use setHeader(name, value) [OK]
Hint: Use setHeader method exactly as shown [OK]
Common Mistakes:
Using header() instead of setHeader()
Using set() or addHeader() which don't exist
Wrong method name casing
3. What will be the value of the Content-Type header sent to the client after running this code snippet?
C. Headers must be set before writing the response body
D. The server.listen port number is invalid
Solution
Step 1: Check order of header setting and response writing
Headers must be set before sending any part of the response body.
Step 2: Identify the error in code order
res.write('Hello') sends body first, so setHeader after it causes error.
Final Answer:
Headers must be set before writing the response body -> Option C
Quick Check:
Set headers before body [OK]
Hint: Set headers before any res.write or res.end [OK]
Common Mistakes:
Setting headers after writing body
Thinking setHeader is invalid method
Assuming res.end needs data always
5. You want to set multiple headers including Content-Type as application/json and Cache-Control as no-cache in a Node.js server. Which code snippet correctly sets both headers before sending the response?
hard
A. res.writeHead(200, {
'Content-Type': 'application/json',
'Cache-Control': 'no-cache'
});
res.end(JSON.stringify(data));
B. res.end(JSON.stringify(data));
res.setHeader('Content-Type', 'application/json');
res.setHeader('Cache-Control', 'no-cache');
C. res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify(data));
res.setHeader('Cache-Control', 'no-cache');
D. res.setHeader({'Content-Type': 'application/json', 'Cache-Control': 'no-cache'});
res.end(JSON.stringify(data));
Solution
Step 1: Understand setting multiple headers at once
writeHead allows setting status and multiple headers before sending response.
Step 2: Check order and correctness
res.writeHead(200, {
'Content-Type': 'application/json',
'Cache-Control': 'no-cache'
});
res.end(JSON.stringify(data)); sets both headers before res.end, correctly using writeHead.
Step 3: Identify errors in other options
res.setHeader({'Content-Type': 'application/json', 'Cache-Control': 'no-cache'});
res.end(JSON.stringify(data)); incorrectly passes an object to setHeader (expects name, value); B and C set headers after res.end (which sends headers).