Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Node.js – dns.lookupService() Method
The dns.lookupService() method resolves a given address and port into a hostname and service. This method uses the operating system's underlying getnameinfo implementation. A TypeError will be thrown if the address is not a valid IP address.
Syntax
dns.lookupService(address, port, callback)
Parameters
address - This parameter takes input for the IP address that needs to be resolved.
port - This parameter takes input for the port number that is attached with the IP address.
callback - It will catch errors, if any.
Example 1
Create a file with the name "lookupService.js" and copy the following code. After creating the file, use the command "node lookupService.js" to run this code as shown in the example below −
// dns.lookupService() Demo Example
// Importing the dns module
const dns = require('dns');
// Passing the IP address and port
dns.lookupService('127.0.0.1', 22, (err, hostname, service) => {
console.log(hostname, service);
});
Output
It will produce the following output −
localhost ssh
Example 2
Let's take another example −
// dns.lookupService() Demo Example
// Importing the dns module
const dns = require('dns');
// Passing the below options to lookup
const options = {
//IPv4
family: 4,
hints: dns.ADDRCONFIG | dns.V4MAPPED,
};
dns.lookup('tutorialsPoint.com',
options, (err, address, family) => {
console.log('address:', address);
if(err){
console.log(err.stack);
} else{
// Calling dns.lookupService() method
// to retrieve details of IP address
dns.lookupService(address, 80,(err, hostname, service) => {
if(err){
console.log(err.stack);
}
// Printing hostname and service
// as callback
console.log(hostname, service);
});
}
});
Output
address: 157.90.94.102 tutorialspoint.com http
Advertisements