0
0
Expressframework~5 mins

req.ip and req.hostname in Express - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does req.ip represent in Express?

req.ip gives you the IP address of the client making the request. It helps you know where the request came from.

Click to reveal answer
beginner
What is the purpose of req.hostname in Express?

req.hostname returns the domain name or host from the request URL. It tells you which website or server the client is trying to reach.

Click to reveal answer
intermediate
How does Express determine req.ip when behind a proxy?

Express looks at the X-Forwarded-For header if trust proxy is enabled. This helps get the real client IP even if the request passes through proxies.

Click to reveal answer
beginner
Is req.hostname affected by the port number in the URL?

No, req.hostname only returns the host name without the port number. For example, from http://example.com:3000, it returns example.com.

Click to reveal answer
intermediate
Why is it important to set app.set('trust proxy', true) when using req.ip behind proxies?

Without trusting the proxy, req.ip might show the proxy's IP instead of the real client IP. Setting trust proxy tells Express to use the correct client IP from headers.

Click to reveal answer
What does req.ip return in Express?
AThe client's IP address
BThe server's IP address
CThe hostname of the request
DThe port number of the request
Which header does Express check to get the real client IP when trust proxy is enabled?
AHost
BX-Forwarded-For
CUser-Agent
DContent-Type
What does req.hostname return?
AThe client's IP address
BThe full URL including protocol
CThe port number
DThe domain name from the request URL
Does req.hostname include the port number if present in the URL?
ANo, it excludes the port number
BYes, always
COnly if the port is 80
DOnly if the port is 443
Why should you set app.set('trust proxy', true) when using req.ip behind a proxy?
ATo block proxy requests
BTo disable IP logging
CTo get the real client IP instead of the proxy IP
DTo enable HTTPS
Explain how req.ip works in Express and how proxies affect it.
Think about how Express finds the client IP when behind other servers.
You got /4 concepts.
    Describe what req.hostname returns and how it differs from the full URL.
    Focus on the host part of the URL only.
    You got /4 concepts.