Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using req.hostname instead of req.ip
Trying to access req.url for IP
✗ Incorrect
The req.ip property in Express gives the client's IP address.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using req.ip instead of req.hostname
Using req.protocol to get hostname
✗ Incorrect
The req.hostname property gives the hostname from the request.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Calling req.ip as a function
Using non-existent methods like req.getIp()
✗ Incorrect
req.ip is a property, not a function, so no parentheses are needed.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping ip and hostname properties
Using req.url or req.method instead
✗ Incorrect
Use req.ip for IP and req.hostname for hostname.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using req.url instead of req.method
Mixing up hostname and url
✗ Incorrect
Use req.ip for IP, req.hostname for hostname, and req.method for HTTP method.