Performance: req.ip and req.hostname
LOW IMPACT
Accessing req.ip and req.hostname affects server-side request handling speed and can impact response time if used inefficiently.
app.use((req, res, next) => {
req.clientIp = req.ip;
req.clientHostname = req.hostname;
// Avoid DNS lookup or perform async caching if needed
next();
});app.use((req, res, next) => {
const ip = req.ip;
const hostname = req.hostname;
// Synchronous DNS lookup for hostname
const dns = require('dns');
let address;
try {
address = dns.lookupSync(hostname);
} catch (err) {
return next(err);
}
req.resolvedAddress = address;
next();
});| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Access req.ip and req.hostname directly | N/A (server-side) | N/A | N/A | [OK] Good |
| Perform synchronous DNS lookup on req.hostname | N/A | N/A | N/A | [X] Bad |