Node.js DNS module

Node.js DNS module

Node.js DNS Module

The Node.js DNS module provides powerful methods for interacting with the Domain Name System. Use it to look up hostnames, resolve DNS records, and reverse-lookup IPs into hostnames.

Node.js DNS module
Node.js DNS module

Complete Advance AI Topics: Click Here
SQL Tutorial:
Click Here

Common DNS Methods

  • dns.lookup(hostname, callback) ÔÇö resolve hostname to IP using OS resolver.
  • dns.resolve4(hostname) / dns.resolve6(hostname) ÔÇö resolve IPv4 / IPv6.
  • dns.resolveMx(hostname) ÔÇö mail-exchange records.
  • dns.resolveCname(hostname) ÔÇö CNAME records.
  • dns.resolveTxt(hostname) ÔÇö TXT records.
  • dns.reverse(ip, callback) ÔÇö reverse lookup IP to hostname(s).
  • dns.lookupService(ip, port, callback) ÔÇö service from IP+port.

Example 1: dns.lookup()

const dns = require('dns');

dns.lookup('www.updategadh.com', (err, address, family) => {
  console.log('address:', address);
  console.log('family:', family);
});

Example 2: resolve4() + reverse()

const dns = require('dns');

dns.resolve4('www.updategadh.com', (err, addresses) => {
  if (err) throw err;
  addresses.forEach((a) => {
    dns.reverse(a, (err, hostnames) => {
      if (err) throw err;
      console.log(`${a} -> ${hostnames}`);
    });
  });
});

Example 3: lookupService()

const dns = require('dns');

dns.lookupService('127.0.0.1', 22, (err, hostname, service) => {
  if (err) throw err;
  console.log(hostname, service);  // e.g. localhost ssh
});

Modern Promise API

const dns = require('dns').promises;

const address = await dns.lookup('www.updategadh.com');
console.log(address);

Download New Real Time Projects:- Click here

Conclusion

The Node.js DNS module is your toolkit for everything DNS ÔÇö lookups, record types, reverse lookups, and service resolution. The promise API makes modern async code clean. For more tutorials, stay tuned to .

node js dns lookup
nodejs dns resolve example
dns module in node js
node dns server
dns – npm
node dns/promises
nodejs dns lookup cache
node js dns w3schools

Written by Rishabh Saini Founder, UpdateGadh

Rishabh Saini is the founder of UpdateGadh. He has 5 years of experience building PHP, Python, Java, AI and ML projects, and works on projects with the latest and trending technologies. He has helped 2,000+ BCA, MCA and B.Tech students with final year projects. Every project on UpdateGadh is built and tested by him before it is published.

Leave a Reply

Your email address will not be published. Required fields are marked *