Created
October 23, 2016 07:03
-
-
Save josephok/9deab3ef5420d706a9b11e86322a3fe0 to your computer and use it in GitHub Desktop.
dns query use nodejs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const print = console.log | |
const dns = require('dns') | |
const hostname = 'v2ex.com' | |
Promise.prototype.success = Promise.prototype.then | |
Promise.prototype.error = Promise.prototype.catch | |
function lookup(hostname) { | |
return new Promise(function (resolve, reject) { | |
dns.lookup(hostname, (err, address, family) => { | |
if (err) { | |
reject(err) | |
} | |
else { | |
resolve(address) | |
} | |
}) | |
}) | |
} | |
lookup(hostname).success(address => { | |
print(`${hostname} has address: ${address}`) | |
}).error(e => print(e.message)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment