Dns

This document describes the Dns client available in the JavaScript environment for app serivices.

Table of content

Object Dns
Functions getHostByName

Object DnsRequest
Functions onresult

Object DnsResult
Members addr
isIPv6

Examples Getting an IP address for a host name
Getting all IP addresses for a host name

Dns

getHostByName
Starts a DNS resolution of a given host name.

Parameters

string hostNameThe hostname to be resolved.
bool allOptional. If true, the result will be an array of all resolved addresses.
int timeoutOptional. The timeout for the request in milliseconds. If no timeout is specified, the default timeout of 5000ms will be used.

Return value

DnsRequestAn object representing the request.
Dns.getHostByName("www.example.com")
    .onresult(function(hostName, result) {
        log(hostName + ": " + (result ? result.addr : "not found"));
     });

DnsRequest

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

DnsRequestA 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"));
     });

DnsResult

addr
A string containing the resolved address.
isIPv6
True, if addr represents an IPv6 address. Otherwise addr represents an IPv4 address.

Examples

Getting an IP address for a host name

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");
    }
});

Getting all IP addresses for a host name

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");
    }
});