0

I'm building a node application and a module of this application checks whether the nameservers of a given domain name point to AWS.

Using the dns module I have the below code:

dns.resolveNs(domain, function (err, hostnames) {
    console.log(hostnames);
    console.log(hostnames.indexOf('awsdns') > -1);
});

hostnames outputs an array of hostnames and the specific domain I've used has the following hostname structure (x4):

ns-xx.awsdns-xx.xxx

But console.log(hostnames.indexOf('awsdns') > -1); returns false?

5
  • 1
    So hostnames is an array? Commented Jul 26, 2015 at 15:39
  • What is the value and type of hostnames?
    – BenM
    Commented Jul 26, 2015 at 15:40
  • hostnames is an array. e.g. ['ns-xx.awsdns-xx.xxx', 'ns-xx.awsdns-xx.xxx', 'ns-xx.awsdns-xx.xxx', 'ns-xx.awsdns-xx.xxx']
    – leaksterrr
    Commented Jul 26, 2015 at 15:41
  • A bit curious why you need the check at all, if all of the entries are known to have that substring...? Commented Jul 26, 2015 at 15:46
  • @T.J.Crowder not all do... obviously. :)
    – leaksterrr
    Commented Jul 26, 2015 at 15:46

2 Answers 2

3

If hostnames is an array, then hostnames.indexOf('awsdns') is looking for an entry that exactly matches (entire string) 'awsdns'.

To look for substrings in the array, use some:

console.log(hostnames.some(function(name) {
    return name.indexOf('awsdns') > -1;
});

Or with ES6 syntax:

console.log(hostnames.some((name) => name.indexOf('awsdns') > -1));

Live Example:

var a = ['12.foo.12', '21.bar.21', '42.foo.42'];

// Fails there's no string that entirely matches 'bar':
snippet.log(a.indexOf('bar') > -1);

// Works, looks for substrings:
snippet.log(a.some(function(name) {
  return name.indexOf('bar') > -1;
}));
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>

1

try

hostnames[0].indexOf('awsdns') > -1;

Since hostnames is an array, you need to check index of an actual hostname, not the array.

Note that this only works because you've said that if any of the entries has the substring, they all will. (Which is extremely unusual.) Otherwise, it would fail if the first entry didn't but a subsequent one did.

8
  • He didn't specify. The point of the my answer is to point out what he was doing wrong, not to write write the code for him since he doesn't specify what exactly he needs to do with this.
    – GPicazo
    Commented Jul 26, 2015 at 15:47
  • Due to the nature of aws nameservers, 'awsdns' will always appear in the first record and there will always only ever be one set of nameservers attached to a domain.
    – leaksterrr
    Commented Jul 26, 2015 at 15:51
  • @leaksterrr: You mean if any of them have it, they'll all have it, but it's possible that you'll have an array where none of them does? (E.g., not related to AWS...) Commented Jul 26, 2015 at 15:53
  • @T.J.Crowder yes, in which case separate handlers are in place for those occurences.
    – leaksterrr
    Commented Jul 26, 2015 at 15:55
  • @leaksterrr: That was really important information to include in the question. Commented Jul 26, 2015 at 15:56

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.