Simple example I think. I want to use Express to return a users IP and hostname upon request. But resolving that hostname from the IP is giving me a little bit of trouble.
router.get('/', (req, res) => {
logRequest(req, res);
});
async function logRequest(req, res) {
res.send({
ip: req.ip,
hostname: await getRemoteHostName(req.ip)
});
}
async function getRemoteHostName(ip) {
await require('dns').reverse(ip, (err, domains) => {
if (err) { handleError(err); return; }
return domains.map(s => s.toLowerCase());
});
}
All I'm getting back is my IP {ip: 192.168.10.100}
when I expect that I should be getting back my hostname too. If I console.log
my hostname it prints to console but the hostname is not sent back by express in the response. What am I doing wrong? I feel like express is sending the response back before dns.reverse
can finish the lookup but I want that lookup to finish and then return the response.