trainingtrains Logo

91-9990449935

 0120-4256464

Node.js DNS

The Node.js DNS module contains methods to get information of given hostname. Let's see the list of commonly used DNS functions:

  • dns.getServers()
  • dns.setServers(servers)
  • dns.lookup(hostname[, options], callback)
  • dns.lookupService(address, port, callback)
  • dns.resolve(hostname[, rrtype], callback)
  • dns.resolve4(hostname, callback)
  • dns.resolve6(hostname, callback)
  • dns.resolveCname(hostname, callback)
  • dns.resolveMx(hostname, callback)
  • dns.resolveNs(hostname, callback)
  • dns.resolveSoa(hostname, callback)
  • dns.resolveSrv(hostname, callback)
  • dns.resolvePtr(hostname, callback)
  • dns.resolveTxt(hostname, callback)
  • dns.reverse(ip, callback)

Node.js DNS Example 1

Let's see the example of dns.lookup() function.

File: dns_example1.js

Open Node.js command prompt and run the following code:

Node.js dns example 1

Node.js DNS Example 2

Let's see the example of resolve4() and reverse() functions.

File: dns_example2.js

Open Node.js command prompt and run the following code:

Node.js dns example 2

Node.js DNS Example 3

Let's take an example to print the localhost name using lookupService() function.

File: dns_example3.js

Open Node.js command prompt and run the following code:

Node.js dns example 3
Next TopicNode.js Net