Overview - req.ip and req.hostname
What is it?
In Express, req.ip and req.hostname are properties of the request object that help identify the client making the request and the host the request was sent to. req.ip gives the IP address of the client, while req.hostname provides the domain name or host header from the request. These properties help servers understand where requests come from and which host they target.
Why it matters
Knowing the client's IP address and the hostname requested is crucial for security, logging, and routing decisions. Without these, servers cannot track users, apply access controls, or serve content correctly for different domains. For example, without req.ip, blocking malicious users or rate limiting is impossible. Without req.hostname, serving multiple websites from one server would be confusing.
Where it fits
Before learning req.ip and req.hostname, you should understand basic Express routing and the request-response cycle. After this, you can explore middleware for security, logging, or multi-tenant hosting that uses these properties to make decisions.