Bird
Raised Fist0
Expressframework~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does res.set do in an Express response?
easy
A. It sets or updates HTTP headers before sending the response.
B. It sends the response body to the client.
C. It closes the server connection immediately.
D. It logs request details to the console.

Solution

  1. Step 1: Understand the purpose of res.set

    res.set is used to add or modify HTTP headers in the response before sending it.
  2. Step 2: Differentiate from other response methods

    Sending the body is done by res.send, not res.set. Logging or closing connections are unrelated.
  3. Final Answer:

    It sets or updates HTTP headers before sending the response. -> Option A
  4. Quick Check:

    res.set sets headers [OK]
Hint: Remember: res.set changes headers, not body or connection [OK]
Common Mistakes:
  • Confusing res.set with res.send
  • Thinking res.set sends the response
  • Assuming res.set closes the connection
2. Which of the following is the correct syntax to set a single header using res.set?
easy
A. res.set('Content-Type', application/json);
B. res.set('Content-Type', 'application/json');
C. res.set({Content-Type: 'application/json'});
D. res.set('Content-Type' => 'application/json');

Solution

  1. Step 1: Recall correct method signature

    The correct syntax for setting one header is res.set(name, value) where both are strings.
  2. Step 2: Check each option

    res.set('Content-Type', 'application/json'); uses correct syntax with two string arguments. res.set('Content-Type' => 'application/json'); uses invalid arrow syntax. res.set({Content-Type: 'application/json'}); uses invalid object syntax - unquoted key Content-Type (hyphen requires quotes). res.set('Content-Type', application/json); misses quotes around value.
  3. Final Answer:

    res.set('Content-Type', 'application/json'); -> Option B
  4. Quick Check:

    Two string args for single header [OK]
Hint: Use two strings for single header: name and value [OK]
Common Mistakes:
  • Using arrow => instead of comma
  • Passing unquoted values
  • Confusing object syntax for single header
3. What will be the value of the Content-Type header after this code runs?
res.set({
  'Content-Type': 'text/html',
  'Cache-Control': 'no-cache'
});
res.set('Content-Type', 'application/json');
medium
A. Undefined (header not set)
B. 'text/html'
C. 'no-cache'
D. 'application/json'

Solution

  1. Step 1: Understand header setting order

    First, Content-Type is set to 'text/html' via object. Then it is overwritten by res.set('Content-Type', 'application/json').
  2. Step 2: Determine final header value

    The last set value for Content-Type is 'application/json', so that is the final header value.
  3. Final Answer:

    'application/json' -> Option D
  4. Quick Check:

    Last header set wins [OK]
Hint: Later res.set calls overwrite previous headers [OK]
Common Mistakes:
  • Assuming first set value stays
  • Confusing other headers with Content-Type
  • Thinking headers merge values
4. Identify the error in this Express code snippet:
app.get('/data', (req, res) => {
  res.send('Hello');
  res.set('X-Custom-Header', '123');
});
medium
A. Headers must be set before sending the response body.
B. res.set cannot set custom headers.
C. res.send should be called twice to set headers.
D. No error; code works fine.

Solution

  1. Step 1: Check order of res.set and res.send

    Headers must be set before calling res.send because sending the body finalizes the response.
  2. Step 2: Identify the mistake

    Here, res.send is called first, then res.set, which is too late to add headers.
  3. Final Answer:

    Headers must be set before sending the response body. -> Option A
  4. Quick Check:

    Set headers before send [OK]
Hint: Always set headers before sending response body [OK]
Common Mistakes:
  • Setting headers after res.send
  • Thinking res.set can't set custom headers
  • Calling res.send multiple times
5. You want to set multiple headers including Content-Type and Cache-Control in one call, but only if a condition is true. Which code correctly uses res.set with condition?
hard
A. res.set({'Content-Type': isJson ? 'application/json' : 'text/html'}); res.set('Cache-Control', 'no-store');
B. res.set('Content-Type', isJson ? 'application/json' : 'text/html', 'Cache-Control', 'no-store');
C. if (isJson) res.set({'Content-Type': 'application/json', 'Cache-Control': 'no-store'});
D. res.set('Content-Type', 'application/json' && 'Cache-Control', 'no-store');

Solution

  1. Step 1: Understand how to set multiple headers conditionally

    Using an object with res.set allows setting many headers at once. Wrapping in an if statement applies condition.
  2. Step 2: Evaluate each option

    if (isJson) res.set({'Content-Type': 'application/json', 'Cache-Control': 'no-store'}); correctly uses an if condition and sets multiple headers in one object. res.set('Content-Type', isJson ? 'application/json' : 'text/html', 'Cache-Control', 'no-store'); has invalid syntax with multiple pairs in one call. res.set({'Content-Type': isJson ? 'application/json' : 'text/html'}); res.set('Cache-Control', 'no-store'); splits headers into two calls but is valid; however, question asks for one call. res.set('Content-Type', 'application/json' && 'Cache-Control', 'no-store'); uses invalid logical operator in arguments.
  3. Final Answer:

    if (isJson) res.set({'Content-Type': 'application/json', 'Cache-Control': 'no-store'}); -> Option C
  4. Quick Check:

    if + res.set(object) for conditional multiple [OK]
Hint: Use object syntax inside if to set multiple headers [OK]
Common Mistakes:
  • Passing multiple header pairs as separate arguments
  • Using logical operators inside res.set arguments
  • Setting headers outside condition
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard