Accessing HTTP Headers with req.headers in Express
📖 Scenario: You are building a simple Express server that needs to read HTTP headers sent by clients. HTTP headers carry important information like user agents or custom tokens.
🎯 Goal: Create an Express server that reads the req.headers object to access HTTP headers and sends back a response showing one specific header value.
📋 What You'll Learn
Create an Express app with a single GET route at '/'
Use req.headers to access the HTTP headers object
Extract the user-agent header from req.headers
Send a response that includes the user-agent header value
💡 Why This Matters
🌍 Real World
Web servers often need to read HTTP headers to understand client details, like browser type or authentication tokens.
💼 Career
Knowing how to access HTTP headers with req.headers is essential for backend developers working with Express to handle requests properly.
Progress0 / 4 steps
1
Set up Express app and import express
Write code to import express and create an Express app instance called app.
Express
Hint
Use require('express') to import Express and call it to create app.
2
Create a GET route at '/'
Add a GET route handler on app for path '/' with parameters req and res.
Express
Hint
Use app.get with path '/' and a callback with req and res.
3
Access the user-agent header from req.headers
Inside the GET route, create a variable called userAgent and set it to req.headers['user-agent'] to get the user agent string.
Express
Hint
Use bracket notation req.headers['user-agent'] to get the header value.
4
Send the user-agent header value in the response
Use res.send to send a string response that says User Agent: followed by the userAgent variable.
Express
Hint
Use template strings with backticks to include userAgent in the response.
Practice
(1/5)
1. What does req.headers represent in an Express app?
easy
A. A function to send headers to the client
B. An object containing all HTTP headers sent by the client
C. A string with the URL path of the request
D. A number representing the HTTP status code
Solution
Step 1: Understand the role of req.headers
req.headers holds all headers sent by the client in the HTTP request.
Step 2: Identify what req.headers contains
It is an object with keys as header names (lowercase) and values as header values.
Final Answer:
An object containing all HTTP headers sent by the client -> Option B
Quick Check:
req.headers = client headers [OK]
Hint: Remember: req.headers holds client headers as an object [OK]
Common Mistakes:
Thinking req.headers is a function
Confusing headers with URL or status code
Expecting headers keys to be uppercase
2. Which of the following is the correct way to access the 'content-type' header from req.headers?
easy
A. req.headers.Content-Type
B. req.headers['Content-Type']
C. req.headers.contentType
D. req.headers['content-type']
Solution
Step 1: Recall header key casing in req.headers
Headers keys are always lowercase in req.headers.
Step 2: Choose the correct syntax to access the header
Use bracket notation with lowercase key: req.headers['content-type'].
Final Answer:
req.headers['content-type'] -> Option D
Quick Check:
Headers keys are lowercase strings [OK]
Hint: Use lowercase keys with bracket notation for headers [OK]
A. Header keys in req.headers are lowercase, so 'Authorization' should be 'authorization'
B. Header keys in req.headers are case-sensitive and 'Authorization' is correct
C. You cannot access headers directly from req.headers
D. res.status() cannot be chained with res.send()
Solution
Step 1: Check header key casing in req.headers
All header keys are lowercase, so 'Authorization' is incorrect.
Step 2: Correct the header key usage
Use req.headers.authorization to access the Authorization header properly.
Final Answer:
Header keys in req.headers are lowercase, so 'Authorization' should be 'authorization' -> Option A
Quick Check:
Headers keys are lowercase strings [OK]
Hint: Always use lowercase keys for req.headers access [OK]
Common Mistakes:
Using uppercase or camelCase header keys
Thinking headers are case-sensitive as sent
Believing res.status() and res.send() can't chain
5. You want to check if a client sent a custom header named 'X-Client-Id' and respond with 'Client ID: <id>'. Which code correctly handles this in Express?