I'm trying to read a site's name from terminal and do a dnslookup to print its IP on screen , but the following code is throwing an error for some reason , here is the code
process.stdout.write('Enter the website name:<www.sitename.com>: ');
process.stdin.resume();
var dns = require('dns');
process.stdin.on('data',function(site) {
var lookup = site.toString('utf-8');
console.log(lookup);
dns.lookup(lookup,function(err,ip) {
if (err) throw err;
console.log(ip);
});
});
The error is if (err) throw err; ^ Error: getaddrinfo ENOENT at errnoException (dns.js:31:11) at Object.onanswer [as oncomplete] (dns.js:123:16)
Why isnt the code working ?
the data on stdin gives you the host with a trailing carriage return. This trailing CR leads to the getaddrinfo ENOENT exception.
modify your code with
var lookup = site.toString('utf-8').trim();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.