0
0
Expressframework~20 mins

res.set for response headers in Express - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Express Headers Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What does this Express code send as response headers?
Consider this Express route handler:
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('{}');
});
ANo headers set, default Content-Type is text/html
BOnly Content-Type: application/json
COnly X-Custom-Header: 12345
DContent-Type: application/json and X-Custom-Header: 12345
Attempts:
2 left
💡 Hint
res.set sets headers before sending the response.
📝 Syntax
intermediate
2: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?
Ares.set(['Content-Type', 'text/plain'], ['Cache-Control', 'no-cache']);
Bres.set({'Content-Type': 'text/plain', 'Cache-Control': 'no-cache'});
Cres.set('Content-Type', 'text/plain', 'Cache-Control', 'no-cache');
Dres.set('Content-Type: text/plain; Cache-Control: no-cache');
Attempts:
2 left
💡 Hint
res.set accepts an object to set multiple headers at once.
🔧 Debug
advanced
2:00remaining
Why does this code cause an error?
Look at this Express code snippet:
res.set();
res.send('Hello');

What error will occur and why?
Express
res.set();
res.send('Hello');
AReferenceError because res is undefined
BSyntaxError due to missing colon in header string
CTypeError because res.set requires both header name and value
DNo error, header is set with empty value
Attempts:
2 left
💡 Hint
Check the parameters required by res.set.
state_output
advanced
2:00remaining
What is the final value of the 'Content-Type' header?
Given this Express code:
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('{}');
Aapplication/json
Btext/html
CBoth headers sent with different values
DNo Content-Type header sent
Attempts:
2 left
💡 Hint
Later calls to res.set overwrite previous headers with the same name.
🧠 Conceptual
expert
2: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');
AAn error is thrown because headers cannot be set after sending
BThe header is sent and overwrites previous headers
CThe header is not sent because response is already finished
DExpress queues the header and sends it after res.send
Attempts:
2 left
💡 Hint
Think about when headers are sent in the HTTP response lifecycle.