Using req.ip and req.hostname in Express
📖 Scenario: You are building a simple Express server that logs and shows the visitor's IP address and the hostname they used to access your site. This helps you understand where your visitors come from and which domain they used.
🎯 Goal: Create an Express server with a route that reads the visitor's IP address using req.ip and the hostname using req.hostname. Then send a response showing these values.
📋 What You'll Learn
Create an Express app
Add a route for GET requests to '/'
Use
req.ip to get the visitor's IP addressUse
req.hostname to get the hostnameSend a response that includes both the IP and hostname
💡 Why This Matters
🌍 Real World
Web servers often need to know the visitor's IP and hostname to customize responses, log traffic, or apply security rules.
💼 Career
Understanding how to access request details like IP and hostname is essential for backend developers working with Express or similar web frameworks.
Progress0 / 4 steps