Chapter 5. Helper APIs

This chapter covers a number of APIs that you’ll almost certainly use regularly but aren’t used as much as those discussed in Chapter 4.

DNS

Programmers, like end users, normally want to refer to things by their domain names instead of their IP addresses. The DNS module provides this lookup facility to you, but it is also used under the hood whenever you are able to use a domain name—for example, in HTTP clients.

The dns module consists of two main methods and a number of convenience methods. The two main methods are resolve(), which turns a domain name into a DNS record, and reverse(), which turns an IP address into a domain. All of the other methods in the dns module are more specialized forms of these methods.

dns.resolve() takes three arguments:

A string containing the domain to be resolved

This can include subdomains, such as www.yahoo.com. The www is technically a hostname, but the system will resolve it for you.

A string containing the types of records being requested

This requires a little more understanding of DNS. Most people are familiar with the “address” or A record type. This type of record maps an IPv4 domain to a domain name (as defined in the previous item). The “canonical name,” or CNAME, records allow you to create an alias of an A record or another CNAME. For example, www.example.com might be a CNAME of the A record at example.com. MX records point to the mail server for a domain for the use of SMTP. When you email person@domain.com, the MX record for domain.com tells your email server where to send their mail. Text records, or TXT, are notes attached to a domain. They have been used for all kinds of functions. The final type supported by this library is service, or SRV, records, which provide information on the services available at a particular domain.

A callback

This returns the response from the DNS server. The prototype will be shown in Example 5-2.

As shown in Example 5-1, calling dns.resolve() is easy, although the callback may be slightly different from other callbacks you’ve used so far.

Example 5-1. Calling dns.resolve( )

dns.resolve('yahoo.com', 'A', function(e,r) { 
  if (e) {
    console.log(e);
  }
  console.log(r); 
} );

We called dns.resolve() with the domain and a record type of A, along with a trivial callback that prints results. The first argument of the callback is an error object. If an error occurs, the object will be non-null, and we can consult it to see what went wrong. The second argument is a list of the records returned by the query.

There are convenience methods for all the types of records listed earlier. For example, rather than calling resolve('example.com', 'MX', callback), you can call resolveMx('example.com', callback) instead (see Example 5-2). The API also provides resolve4() and resolve6() methods, which resolve IPv4 and IPv6 address records, respectively.

Example 5-2. Using resolve( ) versus resolveMx( )

var dns = require('dns');

dns.resolve('example.com', 'MX', function(e, r) {
  if (e) {
    console.log(e);
  }
  console.log(r);
});

dns.resolveMx('example.com', function(e, r) {
  if (e) {
    console.log(e);
  }
  console.log(r);
});

Because resolve() usually returns a list containing many IP addresses, there is also a convenience method called dns.lookup() that returns just one IP address from an A record query (see Example 5-3). The method takes a domain, an IP family (4 or 6), and a callback. However, unlike .dns.resolve(), it always returns a single address. If you don’t pass an address, it defaults to the network interface’s current setting.

Example 5-3. Looking up a single A record with lookup( )

var dns = require('dns');

dns.lookup('google.com', 4, function(e, a) {
  console.log(a);
});

Get Node: Up and Running now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.