get-fqdn

Lookup the fully qualified domain name ("FQDN") of the current server's IP (default) or a custom IP. 90x faster than `hostname -f` and works with Node v6.4+.

Usage no npm install needed!

<script type="module">
  import getFqdn from 'https://cdn.skypack.dev/get-fqdn';
</script>

README

get-fqdn

build status code coverage code style styled with prettier made with lass license npm downloads

Lookup the fully qualified domain name ("FQDN") of the current server's IP (default) or a custom IP. 90x faster than hostname -f and works with Node v6.4+.

Table of Contents

Install

npm:

npm install get-fqdn

yarn:

yarn add get-fqdn

Usage

const getFQDN = require('get-fqdn');

// async/await usage
(async () => {
  try {
    const fqdn = await getFQDN();
    console.log('fqdn', fqdn);
  } catch (err) {
    console.error(err);
  }
});

// then/catch usage
getFQDN().then(fqdn => console.log('fqdn', fqdn)).catch(console.error);

// callback usage
getFQDN((err, fqdn) => {
  if (err) return console.error(err);
  console.log('fqdn', fqdn);
});

Note that you can also pass a custom IP:

const fqdn = await getFQDN('1.1.1.1');
console.log('fqdn', fqdn);

Performance

This package runs approximately 90x faster than the alternative of using hostname -f. It was built to ensure that my project ForwardEmail.net is as optimized as possible. No other NPM packages existed that were similar, as all the others were written in CoffeeScript, used deasync, or used the hostname -f approach (also see the fqdn package that uses the wrong approach too!).

Test #1: uses native os and dns package:

const os = require('os');
const dns = require('dns');

console.time('native nodejs dns lookup');
dns.lookup(os.hostname(), { hints: dns.ADDRCONFIG }, (err, ip) => {
  if (err) throw err;
  dns.lookupService(ip, 0, (err, fqdn) => {
    if (err) throw err;
    console.timeEnd('native nodejs dns lookup');
    console.log('fqdn', fqdn);
    process.exit(0);
  });
});
deploy@mx1:~/test$ node test1.js
native nodejs dns lookup: 1.603ms
fqdn mx1.forwardemail.net

Test #2: uses shelljs and hostname -f:

deploy@mx1:~/test$ node test2.js
fqdn mx1.forwardemail.net
shelljs with hostname -f: 88.311ms

Contributors

Name Website
Nick Baugh http://niftylettuce.com/

License

MIT © Nick Baugh