0
0
Node.jsframework~10 mins

Setting response headers in Node.js - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Setting response headers
Start HTTP request
Create response object
Set response headers
Write response body
Send response to client
End request handling
This flow shows how a Node.js server sets headers on the response before sending it back to the client.
Execution Sample
Node.js
const http = require('http');
const server = http.createServer((req, res) => {
  res.setHeader('Content-Type', 'text/plain');
  res.setHeader('X-Custom-Header', 'MyValue');
  res.end('Hello World');
});
server.listen(3000);
This code creates a simple HTTP server that sets two headers before sending a plain text response.
Execution Table
StepActionHeader SetHeader ValueResponse State
1Start request handling--No headers set
2Set headerContent-Typetext/plainHeaders: {Content-Type: text/plain}
3Set headerX-Custom-HeaderMyValueHeaders: {Content-Type: text/plain, X-Custom-Header: MyValue}
4Write and send response--Response sent with headers and body
5End request handling--Request complete
💡 Response sent and request handling finished
Variable Tracker
VariableStartAfter Step 2After Step 3Final
res.headers{}{Content-Type: 'text/plain'}{Content-Type: 'text/plain', X-Custom-Header: 'MyValue'}{Content-Type: 'text/plain', X-Custom-Header: 'MyValue'}
Key Moments - 2 Insights
Why do we set headers before calling res.end()?
Headers must be set before sending the response body because once res.end() is called, the response is sent and headers cannot be changed. See execution_table steps 3 and 4.
What happens if we set the same header twice?
Setting the same header twice overwrites the previous value. Only the last value is sent. This is why each header key appears once in res.headers as shown in variable_tracker.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what headers are set after step 3?
A{}
B{Content-Type: 'text/html'}
C{Content-Type: 'text/plain', X-Custom-Header: 'MyValue'}
D{X-Custom-Header: 'MyValue'}
💡 Hint
Check the 'Header Set' and 'Response State' columns at step 3 in the execution_table.
At which step is the response body sent to the client?
AStep 4
BStep 3
CStep 2
DStep 5
💡 Hint
Look for the action 'Write and send response' in the execution_table.
If we set a header after calling res.end(), what happens?
AThe header is added successfully
BThe header is ignored because response is already sent
CThe header overwrites previous headers
DThe server crashes
💡 Hint
Refer to key_moments about why headers must be set before res.end() and execution_table steps 4 and 5.
Concept Snapshot
Setting response headers in Node.js:
- Use res.setHeader(name, value) before res.end()
- Headers define metadata like content type
- Setting headers after res.end() has no effect
- Overwriting headers replaces previous values
- Always set all headers before sending response body
Full Transcript
This example shows how a Node.js HTTP server sets response headers before sending the response. The server starts handling a request, sets the Content-Type and a custom header, then sends the response body with res.end(). Headers must be set before calling res.end() because after that the response is sent and headers cannot be changed. Setting the same header twice overwrites the previous value. The execution table tracks each step, showing headers added and when the response is sent. The variable tracker shows how the headers object changes after each setHeader call. This helps beginners understand the order and effect of setting headers in Node.js.