0
0
Expressframework~10 mins

req.ip and req.hostname in Express - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - req.ip and req.hostname
Incoming HTTP Request
Express Middleware
Access req.ip
Get Client IP Address
Access req.hostname
Get Hostname from Request
Use IP and Hostname in App Logic
When Express receives a request, you can get the client's IP using req.ip and the hostname using req.hostname to use in your app.
Execution Sample
Express
app.get('/', (req, res) => {
  const clientIp = req.ip;
  const host = req.hostname;
  res.send(`IP: ${clientIp}, Hostname: ${host}`);
});
This code gets the client's IP and hostname from the request and sends them back in the response.
Execution Table
StepActionreq.ipreq.hostnameResult
1Request arrives from clientundefinedundefinedWaiting for middleware
2Express parses request192.168.1.10example.comreq.ip and req.hostname set
3Handler reads req.ip192.168.1.10example.comClient IP captured
4Handler reads req.hostname192.168.1.10example.comHostname captured
5Response sent192.168.1.10example.comResponse: 'IP: 192.168.1.10, Hostname: example.com'
6Request processing ends192.168.1.10example.comDone
💡 Request handled and response sent with client IP and hostname
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
req.ipundefined192.168.1.10192.168.1.10192.168.1.10192.168.1.10
req.hostnameundefinedexample.comexample.comexample.comexample.com
Key Moments - 2 Insights
Why is req.ip available only after Express parses the request?
Because Express needs to read the incoming request headers and connection info first to determine the IP, as shown in step 2 of the execution_table.
Can req.hostname be different from the actual domain the client typed?
Yes, req.hostname is taken from the Host header sent by the client, so if the client spoofs it, it can differ. See step 2 where req.hostname is set from the request.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of req.ip at step 3?
Aundefined
B192.168.1.10
Cexample.com
Dlocalhost
💡 Hint
Check the 'req.ip' column at step 3 in the execution_table.
At which step does Express set req.hostname?
AStep 2
BStep 1
CStep 4
DStep 5
💡 Hint
Look at the 'req.hostname' column and see when it changes from undefined.
If the client sends a request with Host header 'myapp.local', what changes in the execution_table?
Areq.ip changes to 'myapp.local'
Breq.ip and req.hostname both become 'myapp.local'
Creq.hostname changes to 'myapp.local'
DNo changes, values stay the same
💡 Hint
req.hostname reflects the Host header from the client, as shown in step 2.
Concept Snapshot
req.ip and req.hostname in Express

- req.ip gives the client's IP address
- req.hostname gives the hostname from the Host header
- Both are available after Express parses the request
- Use them to customize responses or logging
- req.hostname depends on client-sent headers
- req.ip can be affected by proxies or network setup
Full Transcript
When an HTTP request arrives in Express, the framework reads the request details. It sets req.ip to the client's IP address and req.hostname to the hostname from the Host header. These values are undefined before parsing. After parsing, handlers can access req.ip and req.hostname to know who sent the request and what hostname was used. This is useful for logging, security, or customizing responses. The IP is usually the client's network address, while hostname is what the client typed or sent in headers. Both are available during request handling.