Skip to content

Instantly share code, notes, and snippets.

@josephok
Created October 23, 2016 07:03
Show Gist options
  • Save josephok/9deab3ef5420d706a9b11e86322a3fe0 to your computer and use it in GitHub Desktop.
Save josephok/9deab3ef5420d706a9b11e86322a3fe0 to your computer and use it in GitHub Desktop.
dns query use nodejs
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