We use req.ip and req.hostname to find out where a web request comes from and which website address was used. This helps us understand and control how users connect to our server.
req.ip and req.hostname in Express
req.ip req.hostname
req.ip gives the IP address of the client making the request.
req.hostname gives the domain name used in the request, without the port number.
app.get('/', (req, res) => { console.log(req.ip); res.send('Your IP is ' + req.ip); });
app.get('/', (req, res) => { console.log(req.hostname); res.send('You visited ' + req.hostname); });
app.get('/', (req, res) => { if (req.hostname === 'example.com') { res.send('Welcome to example.com!'); } else { res.send('Unknown host'); } });
This simple Express server listens on port 3000. When you visit the root URL, it shows your IP address and the hostname you used to visit.
import express from 'express'; const app = express(); const port = 3000; app.get('/', (req, res) => { const clientIp = req.ip; const hostName = req.hostname; res.send(`Hello! Your IP is ${clientIp} and you visited ${hostName}`); }); app.listen(port, () => { console.log(`Server running at http://localhost:${port}/`); });
req.ip may show IPv6 format like ::1 for localhost.
req.hostname does not include the port number, only the domain.
If your app is behind a proxy, you may need to set app.set('trust proxy', true) to get the real client IP.
req.ip tells you the visitor's IP address.
req.hostname tells you the domain name used in the request.
Use these to customize responses or log visitor info in Express apps.
Practice
req.ip represent in an Express app?Solution
Step 1: Understand the purpose of
req.ipreq.ipprovides the IP address of the client making the request.Step 2: Differentiate from
req.hostnamereq.hostnamegives the domain name, not the IP address.Final Answer:
The visitor's IP address -> Option AQuick Check:
req.ip= visitor's IP [OK]
- Confusing
req.ipwithreq.hostname - Thinking
req.ipis server IP - Assuming
req.ipgives HTTP method
Solution
Step 1: Recall Express request object properties
The correct property for the domain name isreq.hostname.Step 2: Check other options for validity
req.host,req.domain, andreq.hostNameare not valid Express properties.Final Answer:
req.hostname -> Option CQuick Check:
Hostname property =req.hostname[OK]
req.hostname [OK]- Using
req.hostinstead ofreq.hostname - Capitalizing property names incorrectly
- Assuming
req.domainexists
http://example.com from IP 192.168.1.5, what will be the response text?Solution
Step 1: Identify
req.ipandreq.hostnamevaluesreq.ipgives the visitor's IP, which is192.168.1.5.req.hostnamegives the domain, which isexample.com.Step 2: Substitute values into the response string
The response string becomes:IP: 192.168.1.5, Hostname: example.com.Final Answer:
IP: 192.168.1.5, Hostname: example.com -> Option DQuick Check:
IP and hostname match request info [OK]
- Swapping IP and hostname values
- Assuming localhost IP instead of visitor IP
- Using undefined or wrong properties
Solution
Step 1: Check property names on
reqreq.ipAddressdoes not exist in Express; the correct property isreq.ip.Step 2: Verify middleware correctness
The middleware callsnext(), so chain continues correctly.Final Answer:
req.ipAddressis not a valid property; should usereq.ip-> Option BQuick Check:
Usereq.ipfor IP address [OK]
req.ip [OK]- Using
req.ipAddressinstead ofreq.ip - Forgetting to call
next() - Trying to log IP from
resobject
trusted.com. Which code snippet correctly uses req.hostname to allow this?Solution
Step 1: Use
app.use((req, res, next) => { if (req.hostname === 'trusted.com') next(); else res.status(403).send('Forbidden'); }); correctly comparesreq.hostnameto check exact domainreq.hostnameto the string 'trusted.com'.Step 2: Evaluate other options for correctness
app.use((req, res, next) => { if (req.ip === 'trusted.com') next(); else res.status(403).send('Forbidden'); }); wrongly comparesreq.ipto a domain string. app.use((req, res, next) => { if (req.host === 'trusted.com') next(); else res.status(403).send('Forbidden'); }); uses invalidreq.host. app.use((req, res, next) => { if (req.hostname.includes('trusted.com')) next(); else res.status(403).send('Forbidden'); }); usesincludeswhich may allow subdomains, not exact match.Final Answer:
Option A code snippet -> Option AQuick Check:
Exact match withreq.hostname= app.use((req, res, next) => { if (req.hostname === 'trusted.com') next(); else res.status(403).send('Forbidden'); }); [OK]
req.hostname === 'domain' [OK]- Comparing IP to domain string
- Using invalid property
req.host - Using partial match instead of exact domain
Ready to prove it?
Pass the test and this pattern moves unknown
