0
0
Expressframework~10 mins

req.headers for HTTP headers in Express - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - req.headers for HTTP headers
Client sends HTTP request
Express receives request
req object created
req.headers populated with HTTP headers
Access req.headers in route handler
Use header values for logic or response
When Express gets a request, it creates req.headers with all HTTP headers sent by the client. You can read these headers inside your route handler.
Execution Sample
Express
app.get('/', (req, res) => {
  console.log(req.headers);
  res.send('Headers logged');
});
This code logs all HTTP headers from the incoming request to the console.
Execution Table
StepActionreq.headers contentResult
1Client sends GET / with headers: Host, User-Agent, Accept{host: 'localhost:3000', 'user-agent': 'Mozilla/5.0', accept: 'text/html'}Express receives request with headers
2Express creates req object and fills req.headers{host: 'localhost:3000', 'user-agent': 'Mozilla/5.0', accept: 'text/html'}req.headers ready for use
3Route handler runs and logs req.headers{host: 'localhost:3000', 'user-agent': 'Mozilla/5.0', accept: 'text/html'}Headers printed to console
4Response sent with message 'Headers logged'N/AClient receives response
5No more stepsN/ARequest handling complete
💡 Request handled fully; req.headers accessed and response sent
Variable Tracker
VariableStartAfter Step 2After Step 3Final
req.headersundefined{host: 'localhost:3000', 'user-agent': 'Mozilla/5.0', accept: 'text/html'}{host: 'localhost:3000', 'user-agent': 'Mozilla/5.0', accept: 'text/html'}Same as after Step 3
Key Moments - 2 Insights
Why does req.headers contain all headers in lowercase keys?
Express converts all header names to lowercase in req.headers for consistency, as shown in the execution_table rows where keys like 'User-Agent' appear as 'user-agent'.
Can I modify req.headers to change client headers?
No, req.headers is read-only for the incoming request. Modifying it won't affect the client or the request itself, as the execution_table shows it only being read.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at Step 2. What does req.headers contain?
AOnly the Host header
BAn empty object
CAn object with all HTTP headers sent by the client
DA string of headers
💡 Hint
Check the 'req.headers content' column at Step 2 in the execution_table
At which step does the route handler log the headers?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
Look for the action mentioning logging req.headers in the execution_table
If the client sends a new header 'X-Test: 123', how would req.headers change?
AIt would replace all headers with only 'x-test'
BIt would include 'x-test': '123' in req.headers
CIt would ignore the new header
DIt would cause an error
💡 Hint
Refer to how req.headers contains all client headers in the execution_table rows
Concept Snapshot
req.headers holds all HTTP headers from the client request.
Keys are lowercase strings.
Access it inside route handlers via req.headers.
It is read-only and reflects client-sent headers.
Use it to read info like user-agent, cookies, or custom headers.
Full Transcript
When a client sends an HTTP request to an Express server, Express creates a request object named req. This object includes a property called headers, which contains all the HTTP headers sent by the client. These headers are stored as key-value pairs with lowercase keys for consistency. Inside your route handler, you can access req.headers to read these headers. For example, you might log them or use them to customize your response. The req.headers object is read-only and reflects exactly what the client sent. This process happens automatically when Express receives the request and before your route handler runs.