Code Monkey home page Code Monkey logo

get-fqdn's Introduction

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

get-fqdn's People

Contributors

niftylettuce avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.