npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

oa-nodejs-internship-lesson1-get-ip-info

v1.1.0-alpha

Published

Returns info about current IP address in Node.js

Downloads

8

Readme

oa-nodejs-internship-lesson1-get-ip-info

Node.js utility to get IPv4 or IPv6 addresses of a current machine.

Basic Usage

const {
    getIpAddress,
    getExternalIpAddress,
    getExternalIpAddressPromise,
} = require('oa-nodejs-internship-lesson1-get-ip-info');

const ipv4Address = getIpAddress(); // e.g. '192.0.2.146'

Or you can use callback-based getExternalIpAddress function to get the IP that is seen when you make any HTTP request. This might be useful when first variant gives you LAN IP instead of WAN IP.

The getExternalIpAddressPromise is equivalent to the getExternalIpAddress but is it's promisified version, so first param (callback) is omitted.

// if you like callbacks
getExternalIpAddress((ipv4Address) => {
    console.log(ipv4Address); // e.g. '103.114.98.206'
});

// if you like Promises
getExternalIpAddressPromise()
    .then((ipv4Address) => {
        console.log(ipv4Address); // e.g. '103.114.98.206'
    });

If for some reason it's impossible to specify the IP address, '0.0.0.0' is returned as result from any of functions.

Advanced Usage

The getIpAddress accepts one optional family string param, which can be either 'IPv4' (default) or 'IPv6'.

Note: But if you're using Node.js version 18.x or higher, this param should be numeric 4 or 6 accordingly, and should be passed always. Check Node.js v18.x OS docs.

const ipv6Address = getIpAddress('IPv6');
//=> e.g. '2001:0db8:85a3:0000:0000:8a2e:0370:7334'

// but for Node.js v18.x or higher
const ipv6Address = getIpAddress(6);

The getExternalIpAddress accepts one optional apiHttpOptions array params, defaults to empty array [], which is an additional list of Node.js HTTP Request Options that should be used before predefined list of Public IP Address API services: api.ipify.org, ipinfo.io/ip, icanhazip.com, ident.me -- in given order.

When request to one API fails, attempt to get IP from the next is made, and down to the last/success or to default '0.0.0.0' fallback value.

Only text/plain or text/html response content-types are supported. No caching supported, and no multiple concurrent use advised.

getExternalIpAddress((ipv4Address) => {
    console.log(ipv4Address); // 'https://ipapi.co/ip' was used as first attempt to get IP
}, [{ host: 'ipapi.co', path: '/ip', port: 443 }]);

// or if you like Promises
getExternalIpAddressPromise([{ host: 'ident.me' }])
    .then((ipv4Address) => {
        console.log(ipv4Address); // 'http://ident.me' was used as first attempt to get IP
    });

For more examples see usage examples.

Note: By default 'http:' protocol is used (80 port). If your service uses 'https:' protocol you need to specify that in the apiHttpOptions array's item by passing port: 443 (as in example above) or protocol: 'https:'. If crypto module is disabled in the current Node.js build, no error will be shown.

Note: The family/version of returned IP (IPv4 or IPv6) fully depends on the passed API service options. By default it's an IPv4.

License

ISC