Challenge - 5 Problems
Express Headers Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What does this Express code send as response headers?
Consider this Express route handler:
What headers will the client receive?
app.get('/test', (req, res) => {
res.set('Content-Type', 'application/json');
res.set('X-Custom-Header', '12345');
res.send('{}');
});What headers will the client receive?
Express
app.get('/test', (req, res) => { res.set('Content-Type', 'application/json'); res.set('X-Custom-Header', '12345'); res.send('{}'); });
Attempts:
2 left
💡 Hint
res.set sets headers before sending the response.
✗ Incorrect
The res.set method sets the specified headers. Here, two headers are set before sending the response, so both appear in the response headers.
📝 Syntax
intermediate2:00remaining
Which option correctly sets multiple headers using res.set?
You want to set Content-Type to 'text/plain' and Cache-Control to 'no-cache' in one call to res.set. Which code is correct?
Attempts:
2 left
💡 Hint
res.set accepts an object to set multiple headers at once.
✗ Incorrect
res.set accepts either a header name and value or an object with multiple headers. Only option B uses the correct object syntax.
🔧 Debug
advanced2:00remaining
Why does this code cause an error?
Look at this Express code snippet:
What error will occur and why?
res.set();
res.send('Hello');What error will occur and why?
Express
res.set(); res.send('Hello');
Attempts:
2 left
💡 Hint
Check the parameters required by res.set.
✗ Incorrect
res.set requires either a header name and value or an object with multiple headers. Calling res.set() with no arguments causes a TypeError.
❓ state_output
advanced2:00remaining
What is the final value of the 'Content-Type' header?
Given this Express code:
What will be the Content-Type header sent to the client?
res.set('Content-Type', 'text/html');
res.set('Content-Type', 'application/json');
res.send('{}');What will be the Content-Type header sent to the client?
Express
res.set('Content-Type', 'text/html'); res.set('Content-Type', 'application/json'); res.send('{}');
Attempts:
2 left
💡 Hint
Later calls to res.set overwrite previous headers with the same name.
✗ Incorrect
The second res.set call overwrites the first Content-Type header value. Only 'application/json' is sent.
🧠 Conceptual
expert2:00remaining
What happens if you call res.set after res.send?
In Express, what is the effect of calling res.set('X-Test', 'value') after res.send('done') has already been called?
Express
res.send('done'); res.set('X-Test', 'value');
Attempts:
2 left
💡 Hint
Think about when headers are sent in the HTTP response lifecycle.
✗ Incorrect
Once res.send is called, the response headers have already been sent to the client. Subsequent calls to res.set will throw an error because headers cannot be set after they are sent.