res.set lets you add or change headers in the response your server sends. Headers give extra info about the response.
res.set for response headers in Express
res.set(field, value) // or res.set(object)
You can set one header by giving a field name and value.
Or set many headers at once by passing an object with key-value pairs.
res.set('Content-Type', 'application/json')
res.set({ 'Cache-Control': 'no-cache', 'X-Powered-By': 'Express' })
This Express app sets headers before sending a plain text response. It shows how to use res.set with both single and multiple headers.
import express from 'express'; const app = express(); app.get('/', (req, res) => { res.set('Content-Type', 'text/plain'); res.set({ 'X-Custom-Header': 'MyValue', 'Cache-Control': 'no-store' }); res.send('Hello, headers set!'); }); app.listen(3000, () => { console.log('Server running on http://localhost:3000'); });
Headers must be set before sending the response body with res.send or res.json.
Header names are case-insensitive but usually written in standard format like 'Content-Type'.
Using res.set does not send the response immediately; it just prepares headers.
res.set adds or changes headers in the HTTP response.
You can set one header or many at once using an object.
Always set headers before sending the response body.
Practice
res.set do in an Express response?Solution
Step 1: Understand the purpose of res.set
res.setis used to add or modify HTTP headers in the response before sending it.Step 2: Differentiate from other response methods
Sending the body is done byres.send, notres.set. Logging or closing connections are unrelated.Final Answer:
It sets or updates HTTP headers before sending the response. -> Option AQuick Check:
res.set sets headers [OK]
- Confusing res.set with res.send
- Thinking res.set sends the response
- Assuming res.set closes the connection
res.set?Solution
Step 1: Recall correct method signature
The correct syntax for setting one header isres.set(name, value)where both are strings.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 keyContent-Type(hyphen requires quotes). res.set('Content-Type', application/json); misses quotes around value.Final Answer:
res.set('Content-Type', 'application/json'); -> Option BQuick Check:
Two string args for single header [OK]
- Using arrow => instead of comma
- Passing unquoted values
- Confusing object syntax for single header
Content-Type header after this code runs?
res.set({
'Content-Type': 'text/html',
'Cache-Control': 'no-cache'
});
res.set('Content-Type', 'application/json');Solution
Step 1: Understand header setting order
First,Content-Typeis set to 'text/html' via object. Then it is overwritten byres.set('Content-Type', 'application/json').Step 2: Determine final header value
The last set value forContent-Typeis 'application/json', so that is the final header value.Final Answer:
'application/json' -> Option DQuick Check:
Last header set wins [OK]
- Assuming first set value stays
- Confusing other headers with Content-Type
- Thinking headers merge values
app.get('/data', (req, res) => {
res.send('Hello');
res.set('X-Custom-Header', '123');
});Solution
Step 1: Check order of res.set and res.send
Headers must be set before callingres.sendbecause sending the body finalizes the response.Step 2: Identify the mistake
Here,res.sendis called first, thenres.set, which is too late to add headers.Final Answer:
Headers must be set before sending the response body. -> Option AQuick Check:
Set headers before send [OK]
- Setting headers after res.send
- Thinking res.set can't set custom headers
- Calling res.send multiple times
Content-Type and Cache-Control in one call, but only if a condition is true. Which code correctly uses res.set with condition?Solution
Step 1: Understand how to set multiple headers conditionally
Using an object withres.setallows setting many headers at once. Wrapping in an if statement applies condition.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.Final Answer:
if (isJson) res.set({'Content-Type': 'application/json', 'Cache-Control': 'no-store'}); -> Option CQuick Check:
if + res.set(object) for conditional multiple [OK]
- Passing multiple header pairs as separate arguments
- Using logical operators inside res.set arguments
- Setting headers outside condition
Ready to prove it?
Pass the test and this pattern moves unknown
