0
0
Expressframework~10 mins

req.ip and req.hostname in Express - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to get the client's IP address using Express.

Express
app.get('/client-ip', (req, res) => {
  const clientIp = req.[1];
  res.send(`Client IP is: ${clientIp}`);
});
Drag options to blanks, or click blank then click option'
Aip
Bhostname
Cpath
Durl
Attempts:
3 left
💡 Hint
Common Mistakes
Using req.hostname instead of req.ip
Trying to access req.url for IP
2fill in blank
medium

Complete the code to get the hostname from the request in Express.

Express
app.get('/host', (req, res) => {
  const hostName = req.[1];
  res.send(`Hostname is: ${hostName}`);
});
Drag options to blanks, or click blank then click option'
Ahostname
Bip
Cprotocol
Dmethod
Attempts:
3 left
💡 Hint
Common Mistakes
Using req.ip instead of req.hostname
Using req.protocol to get hostname
3fill in blank
hard

Fix the error in accessing the client's IP address in this Express route.

Express
app.get('/fix-ip', (req, res) => {
  const clientIp = req.[1];
  res.send(`IP: ${clientIp}`);
});
Drag options to blanks, or click blank then click option'
AgetIp
Bhostname
Cip
DipAddress
Attempts:
3 left
💡 Hint
Common Mistakes
Calling req.ip as a function
Using non-existent methods like req.getIp()
4fill in blank
hard

Fill both blanks to create a route that sends both IP and hostname.

Express
app.get('/info', (req, res) => {
  const ip = req.[1];
  const host = req.[2];
  res.send(`IP: ${ip}, Hostname: ${host}`);
});
Drag options to blanks, or click blank then click option'
Aip
Bhostname
Curl
Dmethod
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping ip and hostname properties
Using req.url or req.method instead
5fill in blank
hard

Fill all three blanks to log IP, hostname, and request method in Express.

Express
app.use((req, res, next) => {
  console.log('IP:', req.[1]);
  console.log('Host:', req.[2]);
  console.log('Method:', req.[3]);
  next();
});
Drag options to blanks, or click blank then click option'
Aip
Bhostname
Cmethod
Durl
Attempts:
3 left
💡 Hint
Common Mistakes
Using req.url instead of req.method
Mixing up hostname and url