0
0
Expressframework~10 mins

res.set for response headers in Express - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - res.set for response headers
Request received
Call res.set(key, value)
Set header in response object
Send response with headers
Client receives headers + body
When a request comes in, calling res.set adds headers to the response before sending it to the client.
Execution Sample
Express
app.get('/hello', (req, res) => {
  res.set('Content-Type', 'text/plain');
  res.set('X-Custom-Header', '12345');
  res.send('Hello World');
});
This code sets two headers on the response and then sends a plain text message.
Execution Table
StepActionHeader KeyHeader ValueResponse Headers State
1Call res.setContent-Typetext/plain{"Content-Type":"text/plain"}
2Call res.setX-Custom-Header12345{"Content-Type":"text/plain","X-Custom-Header":"12345"}
3Call res.sendN/AN/AHeaders sent with body 'Hello World'
💡 Response sent to client with headers and body, ending request cycle.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3
responseHeaders{}{"Content-Type":"text/plain"}{"Content-Type":"text/plain","X-Custom-Header":"12345"}Sent to client
Key Moments - 2 Insights
Why do headers appear only after calling res.send?
Headers set by res.set are stored internally and sent only when res.send is called, as shown in step 3 of the execution_table.
Can I call res.set multiple times for different headers?
Yes, each call adds or updates headers in the responseHeaders object, as seen in steps 1 and 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the responseHeaders state after step 2?
A{"Content-Type":"text/plain","X-Custom-Header":"12345"}
B{"Content-Type":"text/html"}
C{}
DHeaders sent with body
💡 Hint
Check the Response Headers State column at step 2 in the execution_table.
At which step are the headers actually sent to the client?
AStep 1
BStep 3
CStep 2
DHeaders are never sent
💡 Hint
Look at the Action column and Response Headers State at step 3.
If you call res.set('Content-Type', 'application/json') after step 2 but before step 3, what changes?
AHeaders are sent immediately
BX-Custom-Header is removed
CContent-Type header changes to 'application/json'
DNo change until res.send is called
💡 Hint
res.set updates headers before sending; check variable_tracker for header updates.
Concept Snapshot
res.set(key, value) sets a response header.
Multiple calls add or update headers.
Headers are sent only when res.send or res.end is called.
Use res.set before sending response.
Headers control metadata like content type or custom info.
Full Transcript
When an Express server receives a request, you can use res.set to add headers to the response. Each call to res.set adds or updates a header key and value inside the response object. These headers are stored internally and are not sent immediately. Only when you call res.send or res.end does Express send the headers along with the response body to the client. For example, setting 'Content-Type' to 'text/plain' and a custom header 'X-Custom-Header' to '12345' will prepare these headers. When res.send('Hello World') is called, the headers and body are sent together. This process ensures headers are complete before sending. You can call res.set multiple times to add many headers. Changing a header key updates its value before sending. Understanding this flow helps you control response metadata effectively.