Node.js DNS module
Node.js DNS module
The Node.js DNS module provides powerful methods to interact with the Domain Name System (DNS). It allows developers to perform hostname lookups, resolve records, and even reverse IP addresses into hostnames.
Introduction to Applied AI:–Click Here
Below are some of the commonly used DNS functions in Node.js:
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)
Data Science Tutorial:-Click Here
Node.js DNS Example 1
Here’s a simple example using the dns.lookup()
function:
Download New Real Time Projects :-Click here
File: dns_example1.js
const dns = require('dns');
dns.lookup('www.updategadh.com', (err, addresses, family) => {
console.log('addresses:', addresses);
console.log('family:', family);
});
Run the file from the Node.js command line:
node dns_example1.js
Node.js DNS Example 2
Now, let’s use dns.resolve4()
and dns.reverse()
to resolve IPv4 addresses and then perform a reverse lookup:
Machine Learning Tutorial:–Click Here
File: dns_example2.js
const dns = require('dns');
dns.resolve4('www.updategadh.com', (err, addresses) => {
if (err) throw err;
console.log(`addresses: ${JSON.stringify(addresses)}`);
addresses.forEach((a) => {
dns.reverse(a, (err, hostnames) => {
if (err) throw err;
console.log(`reverse for ${a}: ${JSON.stringify(hostnames)}`);
});
});
});
Run the file:
node dns_example2.js
Node.js DNS Example 3
The dns.lookupService()
method can be used to retrieve the hostname and service for a given IP address and port.
Complete Advance AI topics:-Â CLICK HERE
File: dns_example3.js
const dns = require('dns');
dns.lookupService('127.0.0.1', 22, (err, hostname, service) => {
if (err) throw err;
console.log(hostname, service);
// Example output: localhost ssh
});
Run the file:
node dns_example3.js
This article gave you a quick walkthrough of how the Node.js DNS module can be used for hostname lookups, resolving IP addresses, and reverse lookups.
Deep Learning Tutorial:– Click Here
Complete Python Course with Advance topics:-Click Here
SQL Tutorial :–Click Here
node js dns lookup
nodejs dns resolve example
dns module in node js
node dns server
dns – npm
node dns/promises
npm node dns
nodejs dns lookup cache
node js dns server
node js dns w3schools
node js dns example
Post Comment