Dns
This document describes the Dns client available in the JavaScript environment for app serivices.
Table of content
getHostByName
Starts a DNS resolution of a given host name.
Parameters
string hostName | The hostname to be resolved. |
bool all | Optional. If true, the result will be an array of all resolved addresses. |
int timeout | Optional. The timeout for the request in milliseconds. If no timeout is specified, the default timeout of 5000ms will be used. |
Return value
Dns.getHostByName("www.example.com")
.onresult(function(hostName, result) {
log(hostName + ": " + (result ? result.addr : "not found"));
});
onresult
Sets a callback function for processing the response from the DNS server. The callback must be set synchronously after calling Dns.getHostByName.
Note that depending on the parameter all, the result will be
Parameters, if all is set to false
function callback(string hostName, DnsResult result) | A callback that will be called the DNS request is complete. |
Parameters, if all is set to true
function callback(string hostName, [DnsResult]) | A callback that will be called the DNS request is complete. |
Return value
DnsRequest | A reference to the object itself. Useful for method chaining. |
Dns.getHostByName("www.example.com")
.onresult(function(hostName, result) {
log(hostName + ": " + (result ? result.addr : "not found"));
});
addr
A string containing the resolved address.
isIPv6
True, if addr represents an IPv6 address. Otherwise addr represents an IPv4 address.
Examples
In this example we do a DNS resolution of a host name and print the resolved address to the log.
Dns.getHostByName("www.example.com").onresult(function(hostName, result) {
log("DNS resolution for " + hostName + ":");
if (result) {
log((result.isIPv6 ? " IPv6: " : " IPv4: ") + result.addr);
}
else {
log(" host not found");
}
});
In this example we do a DNS resolution of a host name and print all resolved addresses to the log.
Dns.getHostByName("www.example.com", true).onresult(function(hostName, result) {
log("DNS resolution for " + hostName + ":");
if (result.length) {
result.forEach(function(r) {
log((r.isIPv6 ? " IPv6: " : " IPv4: ") + r.addr);
});
}
else {
log(" host not found");
}
});